I attempted to define an array of functions using the code below, but encountered issues with retrieving the value of 'i' outside of the loop.
<script>
var f = []
for (var i=0; i<1000; i++){
f[i] = function(){
return i
}
}
console.log(f[3]);
</script>
Instead of resorting to a brute-force method of writing 1000 lines of code to define the functions, I am curious if there are alternative approaches to tackle this problem. I faced a similar issue in Java, which is discussed in this Stack Overflow post about Array of function pointers in Java. Any insights in either Java or JS would be greatly appreciated.