0

I have some code:

function generateSteps() {
    var steps = {};
    for (var i = 0; i < 3; i++) {
        var step = function () {
            console.log(i);
        };

        steps[i] = step;
    }
    return steps;
  }
var myStep = generateSteps();

And I have outputs:

myStep[0](); // 0 need
myStep[1](); // 1 need
myStep[2](); // 2 need

but my code show me this result:

3 3 3

  • misread the question ... user `for(let i = 0...` etc for a block scoped `i` as opposed to a function scoped `i` which WILL be 3 by the time you run those functions – Jaromanda X Jul 01 '21 at 03:05

0 Answers0