I have this question in an interview
Using javascript, write an array of n elements. Each element in the array is a function that calls console.log of its own index. For eg, the first element will console.log 0, second element will console.log 1....
Thanks in advance
Edit: Sorry for asking before researching. Im new to programming & dont know what closure is & couldnt find something in search. I got the answer myself but here is the problem I had. I wrote something like this
n = 10
var array = []
for (var i=0; i < n; i ++) {
array[i] = function() {
console.log(i);
}
}
for (var j=0; j < n; j ++) {
array[j]()
}