javascript how to fix pitfalls counter [duplicate] - javascript

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.

Related

starting from 10, print odd numbers using JavaScript [duplicate]

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)
}
}

Javascript loop showing same value 10 times [duplicate]

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);
}

How to add delay using setTimeout for every iteration? [duplicate]

This question already has answers here:
How do I add a delay in a JavaScript loop?
(32 answers)
Closed 7 years ago.
This creates a 2 second delay and runs the loop. I need to create a 2 second delay for every iteration, not just once.
var myArray = ['test1','test2','test3'];
function doSetTimeout(index) {
setTimeout(function() { console.log(myArray[index]) }, 2000);
}
var index;
for (index = 0; index < myArray.length; ++index) {
doSetTimeout(index)
}
Expected result would be:
test1
(2 second delay)
test2
(2 second delay)
test3
Just multiply your delay by the index
var myArray = ['test1','test2','test3'];
function doSetTimeout(index) {
setTimeout(function() { console.log(myArray[index]) }, index * 2000;
}
var index;
for (index = 0; index < myArray.length; ++index) {
doSetTimeout(index)
}

Javascript Closure and for loops [duplicate]

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);
}

How to show current loop iteration in click function, javascript [duplicate]

This question already has answers here:
JavaScript closure inside loops – simple practical example
(44 answers)
Closed 8 years ago.
JSFiddle
var arr = [ [0], [1], [2], [3] ];
for ( var i = 0; i < arr.length; i++ ) {
$('#btn-' + i).click(function() {
console.log(i);
});
}
When I'm clicking on corresponding button console.log always shows me last iteration instead of the current iteration. Why?
Try creating a closure, In other words, create a scope per iteration. Now in your code all the event handlers are created in a single scope and the i inside of that scope would get updated instantly to 4. So as a result, when you clicking on all the buttons the result would be same. That is the updated one of i
for ( var i = 0; i < arr.length; i++ ) {
var j = function(x) {
$('#btn-' + x).click(function() {
console.log(x);
});
}
j(i);
}
DEMO
Because of closure! For that you can do this:
for (var i = 0; i < arr.length; i++) {
(function(n) {
$('#btn-' + i).click(function() {
console.log(n);
});
})(i);
}
DEMO

Categories