1

Making an attempt to teach coding to kids. I am trying to create a layer of abstraction over javascript just to simplify the syntax for the kids.

I think it will be a bit overwhelming for a kid to learn a typical while loop

Is there any way to convert

var i = 0 
while(i<10)
{ 
doThis(); 
doThat() 
i++
}

into something easier like this

repeat(10)
{
doThis();
doThat();
}

Thanks

12
  • 1
    Unless you code your own compiler, no Commented Aug 11, 2020 at 20:16
  • 2
    "I think it will be a bit overwhelming for a kid to learn a typical while loop" You'd be surprised what kids can learn Commented Aug 11, 2020 at 20:17
  • 1
    @j08691 This is motivating. I am breaking my head to make things simple Commented Aug 11, 2020 at 20:19
  • 1
    With that syntax, @epascarello is correct, you'd need your own interpreter to transpile it into vanilla JavaScript. Alternatively, if syntax isn't an issue, you could just write a function called repeat where it's essentially just a wrapper for a loop. Commented Aug 11, 2020 at 20:19
  • 2
    I know that you're trying to make it easier for the kids to learn coding, but at the same time, how helpful will it be for them once they pickup your customized logic/structure to then eventually go "ok, forget all that. here is what it actually does". Commented Aug 11, 2020 at 20:40

1 Answer 1

2

I am not sure if it's possible to do exactly what you want, even hacking.

However, if you want to simplify it for your students, you go about with something like this?

/* you can hide this function by having it imported from external JS file */
var repeat = function(num, func){
   var i = 0;
   while(i < num){ 
      func();
      i++;
   }
};

var command = function(){
   doThis(); 
   doThat();
};

repeat(10, command);

if you're working with modern javascript

const repeat = (num, func) => {
   let i = 0;
   while(i < num){ 
      func();
      i++;
   }
};

const command = () => {
   doThis(); 
   doThat();
};

repeat(10, command);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Jacky however there is no flexibility here which while loop has. The command function is set to do doThis and do That. What if we have to repeat only doThis?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.