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]
Loop - increment
javascript - JS loop increment each value by 2 - Stack Overflow
increment in for loop javascript - Stack Overflow
The for loop. Can someone explain to me the increment. i++, for example.
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;
}
}
You're declaring the variable inside the loop, so it happens every time the loop runs. Simply move the declaration outside...
var number = 0;
for (var i = 0; i < 5; i++) {
number++;
console.log(number);
}
It prints 1, 1, 1, 1, 1 because you reset numberin every iteration.
change your code to:
for (var i = 0; i < 5; i++) {
console.log(i);
}
or
var number = 0;
for (var i = 0; i < 5; i++) {
console.log(number);
number++;
}
(Answer to additional question in comments) You get 1,2,3,4,5 because you increment number before you print it.
I kind of understand its purpose but could someone just explain it to me like I'm 5.
I understand the first part of the for loop, I understand the condition. The last part, I still have a hard time wrapping my head around it.