How about this?
/*forLoop takes 4 parameters
1: val: starting value.
2: condition: This is an anonymous function. It is passed the current value.
3: incr: This is also an anonymous function. It is passed the current value.
4: loopingCode: Code to execute at each iteration. It is passed the current value.
*/
var forLoop = function(val, condition, incr, loopingCode){
var loop = function(val, condition, incr){
if(condition(val)){
loopingCode(val);
loop(incr(val), condition, incr);
}
};
loop(val, condition, incr);
}
Then call the loop as follows:
forLoop(0,
function(x){return x<10},
function(x){return ++x;},
function(x){console.log("functional programming is a religion")}
);
Output:
functional programming is a religion
functional programming is a religion
functional programming is a religion
functional programming is a religion
functional programming is a religion
functional programming is a religion
functional programming is a religion
functional programming is a religion
functional programming is a religion
functional programming is a religion
Do let me know What you think about this answer.