This question already has answers here:
JavaScript closure inside loops – simple practical example
(44 answers)
Closed 7 years ago.
The code below returns:
10
10
10
10
10
10
10....
How can I produce the following output?
0 1 2 3 4 5 6 7 8 9
Here is my code:
function go() {
var procedures = [];
for (var i = 0; i < 10; i++) {
procedures[procedures.length] = function () {
alert("You are now " + i + " years old");
};
}
run_procs(procedures);
}
function run_procs(procs) {
for (var i = 0; i < procs.length; i++) {
procs[i]();
}
}
go();
guide me please thanks ...
Wrap it in self-executing anonymous function
for (var i = 0; i < 10; i++) {
(function (i) {
procedures[procedures.length] = function () {
alert("You are now " + i + " years old");
}
})(i);
}
Related
This question already has answers here:
JavaScript closure inside loops – simple practical example
(44 answers)
Closed 4 months ago.
I'm trying to run a for loop and print the current index without use let
here my code:
function init() {
for (var index = 0; index < 5; ++index) {
setTimeout(() => {
console.log(index);
}, index);
}
}
I expected to: 0 1 2 3 4
but i get 5 5 5 5 5
Once the Var replace in Let the problem will be solved
I want to stay with Var
How can the problem be solved?
Thanks
function init() {
for (var index = 0; index < 5; ++index) {
const i = index;
setTimeout(() => {
console.log(i);
}, i);
}
}
This should work.
This question already has answers here:
How to Reverse a for loop? [closed]
(4 answers)
Closed 6 months ago.
How can the loop start writing from 10 to 0.
Currently, the output is 1,3,5,7,9 but, I need it as 9,7,5,3,1
for (var i = 0; i <= 10; i++) {
if (i % 2 != 0) {
console.log(i)
}
}
for (var i = 10; i >= 0; i--) {
if (i % 2 != 0) {
console.log(i)
}
}
This question already has answers here:
async loading javascript with document.write
(5 answers)
How do I add a delay in a JavaScript loop?
(32 answers)
Closed 3 years ago.
I have a code to print a pyramid pattern using callback in javascript.
Right now I am able to load entire code after 2 sec.
I need to print pattern rows after 2 sec interval of time instead loading it at a whole using call back and promises . How can I do it ?
var n = 5;
for (var i = 0; i < n; i++) {
getStar(i, n).then(star => console.log(star));
}
function getStar(i, n) {
return new Promise((resolve, reject) => {
var str = '';
for (var j = 1; j < n - i; j++) {
str = str + ' ';
}
for (var k = 1; k <= (2 * i + 1); k++) {
str = str + '*';
}
setTimeout(() => {
resolve(str);
}, 2000)
})
}
This question already has answers here:
JavaScript closure inside loops – simple practical example
(44 answers)
Closed 6 years ago.
Why is this function only sending the number 10 ten times. I want it to send 1... 2 ... 3 ... 4 ... 5 and so on
but instead its showing
10....10.... 10... 10... I'm not sure why it would.
How do I make a loop that returns distinct values?
for (i = 0; i < locations.length; i++) {
setTimeout(function() { alert("test"+i.toString()) ; }, 100);
}
How do I make a loop that returns distinct values?
You can do this by using a closure (pass i back into an immediately invoked function expression (IIFE)). This will maintain the value of i:
for (var i = 0; i < 10; i++) {
(function(i) {
setTimeout(function() {
console.log("test" + i);
}, 100);
})(i);
}
To increment the timeout by using the i works the same way. Making sure to wrap the entire timeout call with the IIFE:
for (var i = 0; i < 10; i++) {
(function(i) {
setTimeout(function() {
console.log("test" + i);
}, i * 100);
})(i);
}
This question already has answers here:
How do JavaScript closures work?
(86 answers)
JavaScript closure inside loops – simple practical example
(44 answers)
Closed 6 years ago.
I have code on JavaScript.
var a = [];
for (var i = 0; i < 5; i++) {
a[i] = function () {
alert(i);
};
}
a[2]();
If I invoke a[2]() I expect to see a message with 2 but instead of this I see 5.
To fix it I can rewrite it like this:
for (var i = 0; i < 5; i++) {
(function (v) {
a[i] = function () {
alert(v);
}
})(i)
}
But I cannot understand how does it work. So why I need to wrap my function code to closure?