Created
May 23, 2015 12:25
-
-
Save thomasfoster96/5bc909ba2085e1f9f452 to your computer and use it in GitHub Desktop.
ES6 Loop Functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* The until version of doWhileLoop. | |
*/ | |
function untilLoop(condition, body){ | |
if(!condition()){ | |
body(); | |
return untilLoop(condition,body); | |
}else{ | |
return; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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