Question
I want to implement javascript function like for-loop like construct, for_, using first-class functions. for_ takes 4 arguments: - cur: the initial current value -
I want to implement javascript function like for-loop like construct, for_, using first-class functions.
for_ takes 4 arguments:
- cur: the initial current value
- cond: the function that is used to decide whether or not we should execute the "loop" body. The cond function expects an argument whose type is the same as that of cur; the cond function is expected to return a boolean.
- next: the function that is used to compute the "next" current value. Like cond, next expects an argument whose type is the same as that of cur; the next function is expected to return a value of the same type.
- fbody: the function corresponding to the loop body. This function is called with the cur value as an argument.
EXAMPLE OF HOW TO USE for_ FUNCTION:
for_(0, i => i < 10, i => i+1, i => {
console.log(`yay: ${i}`);
});
// is computationally equivalent to:
for(let i = 0; i < 10; i++) {
console.log(`yay: ${i}`);
}
______________________________________________________________________________________________________________________
PLEASE DO NOT USE THINGS LIKE IFS OR WHILE LOOPS. MUST BE RECURSIVE FUNCTION/METHOD
function for_(cur, cond, next, fbody) {
/**
/** **/
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started