Use the += assignment operator:
for (var i = 0; i < myVar.length; i += 3) {
Technically, you can place any expression you'd like in the final expression of the for loop, but it is typically used to update the counter variable.
For more information about each step of the for loop, check out the MDN article.
Answer from Andrew Whitaker on Stack OverflowUse the += assignment operator:
for (var i = 0; i < myVar.length; i += 3) {
Technically, you can place any expression you'd like in the final expression of the for loop, but it is typically used to update the counter variable.
For more information about each step of the for loop, check out the MDN article.
A for loop:
for(INIT; TEST; ADVANCE) {
BODY
}
Means the following:
INIT;
while (true) {
if (!TEST)
break;
BODY;
ADVANCE;
}
You can write almost any expression for INIT, TEST, ADVANCE, and BODY.
Do note that the ++ operators and variants are operators with side-effects (one should try to avoid them if you are not using them like i+=1 and the like):
++imeansi+=1; return ii++meansoldI=i; i+=1; return oldI
Example:
> i=0
> [i++, i, ++i, i, i--, i, --i, i]
[0, 1, 2, 2, 2, 1, 0, 0]
javascript - JS loop increment each value by 2 - Stack Overflow
Increment and decrement
Basic JavaScript: Increment a Number with JavaScript
Question about incrementation when looping.
Videos
You could multiply distanceNorm by i+1 at each iteration:
function setDatePosition(timelineComponents, min) {
for (i = 0; i < timelineComponents['timelineDates'].length; i++) {
var distance = daydiff(timelineComponents['timelineDates'][0], timelineComponents['timelineDates'][i]),
distanceNorm = Math.round(distance/timelineComponents['eventsMinLapse']) + 2;
timelineComponents['timelineEvents'].eq(i).css('left', (distanceNorm*min*i+1)+'px');
}
}
I believe your 20px coming from min?
If that is the case, you just need to increase min after each iteration.
min += 20;
function setDatePosition(timelineComponents, min) {
for (i = 0; i < timelineComponents['timelineDates'].length; i++) {
var distance = daydiff(timelineComponents['timelineDates'][0], timelineComponents['timelineDates'][i]),
distanceNorm = Math.round(distance/timelineComponents['eventsMinLapse']) + 2;
timelineComponents['timelineEvents'].eq(i).css('left', (distanceNorm*min)+'px');
min += 20;
}
}
Hi everyone,
I’m new to JavaScript and I was learning about increment and decrement. I am confused with the following code:
let counter = 0; counter++; ++counter; alert(counter);
Why is the output 2? I would assume the second line would result to 0, and the third line would result to 1 with a final return of 1?
Thank you in advance. I know this is a silly question :(