i just beginner in JS, in all tutorials i saw lot of examples like that, but it for cycle 1, 2, 3 etc.
for (i=1;i<=20;i++)
So, how to create an cycle: 5, 10, 15, 25 etc?
Thnx for any help!
i just beginner in JS, in all tutorials i saw lot of examples like that, but it for cycle 1, 2, 3 etc.
for (i=1;i<=20;i++)
So, how to create an cycle: 5, 10, 15, 25 etc?
Thnx for any help!
Hi maybe something like this
for (var i = 0; i <= 20; i+=5) {
console.log(i);
}
First you need to change how the value of i
is updated. You can do it like this:
i += 5
which is equivalent to
i = i + 5
Of course, instead of 5
you can put the value that you prefer.
Then you have to change where the for
loop starts, by setting
var i = 5
and finally you choose the value at which your loop should stop (for instance, 50). To sum up:
for(var i = 5; i < 50; i +=5)
With the right amount for the incrementor.
var i;
for (i = 5; i <= 20; i += 5) {
document.write(i + '<br>');
}
You can create it like this:
for(var i=5; i<=20; i+=5)
Read it like this:
for ( i=5; i <= 100; i += 5 )
Or:
var j;
for ( i=1; i <= 100; i += 1 ) {
j = i * 5;
// ...
}