function myFunc(num) {
let a = 5;
let b = 6;
let c = 9;
return function() {
return num * a;
}
}
const func = myFunc(2);
console.log(func(2)); //10
console.log(func(2)); //12
console.log(func(2)); //18
console.log(func(2)); //10
function xxx(arr, val) {
let index = -1;
return () => val * arr[index = (index + 1) % arr.length];
}
const f = xxx([ 5, 6, 9 ], 2);
console.log([...Array(10)].map(f)) // [10, 12, 18, 10, 12, 18, 10, 12, 18, 10]
код до вложенной функции менять нельзя
function xxx(num) {
let a = 5;
let b = 6;
let c = 9;
return function() {
[ a, b, c ] = [ b, c, a ];
return num * c;
};
}
const initFunc = (nums) => {
let i = 0;
const { length } = nums;
return (n) => n * nums[i++ % length];
}
const func = initFunc([5, 6, 9]);
console.log(func(2)); //10
console.log(func(2)); //12
console.log(func(2)); //18
console.log(func(2)); //10