0

I need to create a function that return an array that consists of n keys 0..[n-1]. The value of each key is a function that upon calling prints the key via (console.log()). e.g. fun8(5)[3] prints 3.

I tried this:

function fun8(n) {
    var arr = [];
    for (i = 0; i < n; i++) {
        arr[i] = function() {
            console.log(i);
        };
    }
    return arr;
}    

And it doesn't work. Any suggestions please? Thanks!

pherris
  • 17,195
  • 8
  • 42
  • 58

0 Answers0