Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ES6 Loop Functions
/*
* The until version of doWhileLoop.
*/
function untilLoop(condition, body){
if(!condition()){
body();
return untilLoop(condition,body);
}else{
return;
}
}
/*
* Same as whileLoop, except that the body function is run before the condition is checked.
*/
function doWhileLoop(condition, body){
body()
if(condition()){
return doWhileLoop(condition, body);
}else{
return;
}
}
/*
* All parameters are functions. if initially is defined, initially is run, otherwise step is run. if condition is true then body is run, otherwise the function returns.
*/
function forLoop(initially, condition, step, body){
if(initially){
initially();
}else{
step();
}
if(condition()){
body();
return forLoop(undefined, condition, step, body);
}else{
return;
}
}
/*
* Same as whileLoop, but condition must be false to continue the loop.
*/
function untilLoop(condition, body){
if(!condition()){
body();
return untilLoop(condition,body);
}else{
return;
}
}
/*
* Both parameters are functions. If the condition function returns true, whileLoop will run the body function and then return
*/
function whileLoop(condition, body){
if(condition()){
body();
return whileLoop(condition, body);
}else{
return;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment