Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

formatting, grammatical, and spelling improvement(s)
Source Link

YouYour code doesn't work, because what it does is:

Create variable `funcs` and assign it an empty array; 
Loop from 0 up until it is less than 3 and assign it to variable `i`;
 Push to variable `funcs` next function: 
 // Only push (save), but don't execute
 **Write to console current value of variable `i`;**
// First loop has ended, i = 3;
Loop from 0 up until it is less than 3 and assign it to variable `j`;
 Call `j`-th function from variable `funcs`: 
 **Write to console current value of variable `i`;** 
 // Ask yourself NOW! What is the value of i?

Now the question is, what is the value of variable i when the function is called? Because the first loop is created with the condition of i < 3, it stops immediately when the condition is false, so it is i = 3.

You need to understand that, in time when your functions are created, none of their'stheir code is executed, it is only saved for later. And so when they are called later, the interpreter executes them and asks "what: "What is the current value of i"i?"

So, your goal is to first save the value of i to function and only after that save the function to funcs. This could be done for example this way:

var funcs = [];
for (var i = 0; i < 3; i++) { // let's create 3 functions
 funcs[i] = function(x) { // and store them in funcs
 console.log("My value: " + x); // each should log its value.
 }.bind(null, i);
}
for (var j = 0; j < 3; j++) {
 funcs[j](); // and now let's run each one to see
}

This way, each function will have it's own variable 'x'x and we set this x to the value of i in each iteration.

This is only one of the multiple ways how to solve this problem.

You code doesn't work, because what it does is:

Create variable `funcs` and assign it an empty array; 
Loop from 0 up until it is less than 3 and assign it to variable `i`;
 Push to variable `funcs` next function: 
 // Only push (save), but don't execute
 **Write to console current value of variable `i`;**
// First loop has ended, i = 3;
Loop from 0 up until it is less than 3 and assign it to variable `j`;
 Call `j`-th function from variable `funcs`: 
 **Write to console current value of variable `i`;** 
 // Ask yourself NOW! What is the value of i?

Now the question is, what is the value of variable i when the function is called? Because first loop is created with condition i < 3, it stops immediately when the condition is false, so it is i = 3.

You need to understand that, in time when your functions are created, none of their's code is executed, it is only saved for later. And so when they are called later, the interpreter executes them and asks "what is the current value of i"?

So, your goal is to first save value of i to function and only after that save the function to funcs. This could be done for example this way:

var funcs = [];
for (var i = 0; i < 3; i++) { // let's create 3 functions
 funcs[i] = function(x) { // and store them in funcs
 console.log("My value: " + x); // each should log its value.
 }.bind(null, i);
}
for (var j = 0; j < 3; j++) {
 funcs[j](); // and now let's run each one to see
}

This way, each function will have it's own variable 'x' and we set this x to value i in each iteration.

This is only one of multiple ways how to solve this problem.

Your code doesn't work, because what it does is:

Create variable `funcs` and assign it an empty array; 
Loop from 0 up until it is less than 3 and assign it to variable `i`;
 Push to variable `funcs` next function: 
 // Only push (save), but don't execute
 **Write to console current value of variable `i`;**
// First loop has ended, i = 3;
Loop from 0 up until it is less than 3 and assign it to variable `j`;
 Call `j`-th function from variable `funcs`: 
 **Write to console current value of variable `i`;** 
 // Ask yourself NOW! What is the value of i?

Now the question is, what is the value of variable i when the function is called? Because the first loop is created with the condition of i < 3, it stops immediately when the condition is false, so it is i = 3.

You need to understand that, in time when your functions are created, none of their code is executed, it is only saved for later. And so when they are called later, the interpreter executes them and asks: "What is the current value of i?"

So, your goal is to first save the value of i to function and only after that save the function to funcs. This could be done for example this way:

var funcs = [];
for (var i = 0; i < 3; i++) { // let's create 3 functions
 funcs[i] = function(x) { // and store them in funcs
 console.log("My value: " + x); // each should log its value.
 }.bind(null, i);
}
for (var j = 0; j < 3; j++) {
 funcs[j](); // and now let's run each one to see
}

This way, each function will have it's own variable x and we set this x to the value of i in each iteration.

This is only one of the multiple ways to solve this problem.

Source Link
Buksy
  • 12.4k
  • 9
  • 65
  • 70

You code doesn't work, because what it does is:

Create variable `funcs` and assign it an empty array; 
Loop from 0 up until it is less than 3 and assign it to variable `i`;
 Push to variable `funcs` next function: 
 // Only push (save), but don't execute
 **Write to console current value of variable `i`;**
// First loop has ended, i = 3;
Loop from 0 up until it is less than 3 and assign it to variable `j`;
 Call `j`-th function from variable `funcs`: 
 **Write to console current value of variable `i`;** 
 // Ask yourself NOW! What is the value of i?

Now the question is, what is the value of variable i when the function is called? Because first loop is created with condition i < 3, it stops immediately when the condition is false, so it is i = 3.

You need to understand that, in time when your functions are created, none of their's code is executed, it is only saved for later. And so when they are called later, the interpreter executes them and asks "what is the current value of i"?

So, your goal is to first save value of i to function and only after that save the function to funcs. This could be done for example this way:

var funcs = [];
for (var i = 0; i < 3; i++) { // let's create 3 functions
 funcs[i] = function(x) { // and store them in funcs
 console.log("My value: " + x); // each should log its value.
 }.bind(null, i);
}
for (var j = 0; j < 3; j++) {
 funcs[j](); // and now let's run each one to see
}

This way, each function will have it's own variable 'x' and we set this x to value i in each iteration.

This is only one of multiple ways how to solve this problem.

lang-js

AltStyle によって変換されたページ (->オリジナル) /