I would like to create a function returning an array of functions. When any function from the array is called, the result must be the index of this function in the array. The task and challenge are to create this function without for-loops. I was moving this way, however, the code did not work as I expected:
const createArrayOfFunctions = number => {
const functionsArray = new Array(number).fill();
return functionsArray.map(element => function() {
return functionsArray.indexOf(element);
});
};
I expected:
const testArray = createArrayOfFunctions(7);
testArray[2](); // 2;
Is there any way to have the goal achieved without using for-loops. I need it for a study project.