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
asked Aug 11, 2020 at 20:14
Get Set Kode
1111 gold badge1 silver badge4 bronze badges
-
1Unless you code your own compiler, noepascarello– epascarello2020年08月11日 20:16:38 +00:00Commented 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 learnj08691– j086912020年08月11日 20:17:19 +00:00Commented Aug 11, 2020 at 20:17
-
1@j08691 This is motivating. I am breaking my head to make things simpleGet Set Kode– Get Set Kode2020年08月11日 20:19:17 +00:00Commented Aug 11, 2020 at 20:19
-
1With 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.Bren– Bren2020年08月11日 20:19:18 +00:00Commented Aug 11, 2020 at 20:19
-
2I 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".Taplar– Taplar2020年08月11日 20:40:01 +00:00Commented Aug 11, 2020 at 20:40
1 Answer 1
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);
answered Aug 11, 2020 at 20:28
Jacky Lam
772 gold badges2 silver badges7 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
Get Set Kode
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?
lang-js