Skip to content

Instantly share code, notes, and snippets.

@thomasfoster96
Created May 23, 2015 12:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomasfoster96/5bc909ba2085e1f9f452 to your computer and use it in GitHub Desktop.
Save thomasfoster96/5bc909ba2085e1f9f452 to your computer and use it in GitHub Desktop.
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