This is an academic question concerning javascript.
The code segment below takes an array of integers, and returns an array of pointers to functions. The functions pointed to are suppose to return the squared value of the array.
If the input was listOfFunction = sqFnList([1,2,3,4,5])
Then listOfFunction[0]()
should return 1
and listOfFunction[1]()
should return 4
but it does not.
function sqFnList(a){
var b = [];
for ( var i = 0; i <a.length; i++)
{
var sq = a[i] * a[i];
b[i] = function() { return sq;}
}
return b;
}
x = sqFnList([3,4,5])[0]()
The issues is that x = 25. Thanks for the help in advance.