In general, you need to write the function func (a, gen), which takes 2 functions as input, a and gen, where gen is the generator function. func returns a new generator function that, with each call, takes the next value from gen and passes it through function a. Example:
function createCounter(start, step) { return function(){ if (start === undefined) start = 0; if(step === undefined) step = 1; start+=step; return start - step; }; } var counter = createCounter(1, 1); function pow(x) { return x * x; } var powGen = func(pow, counter); console.log(powGen()); // 1 console.log(powGen()); // 4 console.log(powGen()); // 9 console.log(powGen()); // 16 At the same time, it is necessary to make it so that as a gen it was possible to specify a function with arguments, and when calling
function add(a, b) { return a + b; } var powAdd = func(pow, add); console.log(powAdd(1, 4)); // 25 = (1 + 4) ^ 2 console.log(powAdd(2, 5)); // 49 = (2 + 5) ^ 2 One solution:
function func(a, gen) { return function() { return a(gen()); }; } var func1 = func( pow, createCounter(1, 1) ); func1(); // 1 func1(); // 4 func1(); // 9 func1(); // 16 func1(); // 25 But this and other options do not withstand the conditions of the problem. I want to figure out how to write the func function body correctly. Link to the problem book (problem number 4): http://dkab.imtqy.com/jasmine-tests/?spec=4
add- does not return the function generator ??? - Grundy