I carry out a simple solution of problems, but I got caught up with a common mistake and cannot understand why this is so. Please explain. The code is long consisting of 4 parts.

The error occurs in the fourth part ( task-4 ). When checking through typeof , squareGen === 'number' . Why 'number' ?

 // task-1 function sequence(start, step) { step = step || 1; start = start || 0; let flag = false, firstStep = 0; return function() { if (flag) { return start += step; } else { flag = true; return start += firstStep; } } } var generator = sequence(10, 3); var generator2 = sequence(7, 1); var generator3 = sequence(0, 2); // task-2 function take(fn, steps) { let array = []; for (let i = 0; i < steps; i++) { array.push(fn()); } return array; } // task-3 function square(x) { return x * x; } console.log(map(square, [1, 2, 3, 4])); // [1, 4, 9, 16] console.log(map(square, [])); // [] var arr = [1, 2, 3]; console.log(map(square, arr)); // [1, 4, 9] console.log(arr); // [1, 2, 3] function map(fn, array) { let mas = array, newArray = []; mas.forEach(function(el) { newArray.push(fn(el)); }); return newArray; } // task-4 function fmap(a, gen) { let resultOfGen = gen(); return a(resultOfGen); } var gen = sequence(1, 1); var squareGen = fmap(square, gen); squareGen(); console.log(typeof squareGen); console.log(squareGen()); // 1 console.log(squareGen()); // 4 console.log(squareGen()); // 9 console.log(squareGen()); // 16 

  • one
    fmap returns the result of calling function a , which in this case is square . The result of this function is a number, and when you try to call it as a function, you get a logical message - Grundy
  • Thank. Understood) - Slava

0