-4

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!

Andy
  • 61,948
  • 13
  • 68
  • 95
Mark Bg
  • 65
  • 1
  • 6

6 Answers6

3

Hi maybe something like this

for (var i = 0; i <= 20; i+=5) {
  console.log(i);
}
Andy
  • 61,948
  • 13
  • 68
  • 95
azibi
  • 4,027
  • 1
  • 14
  • 17
2

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)
cholo14
  • 594
  • 1
  • 3
  • 22
1

With the right amount for the incrementor.

var i;
for (i = 5; i <= 20; i += 5) {
    document.write(i + '<br>');
}
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0
for (i=5;i<=20;i=i+5)

I think you are looking for this

Mustafa Mamun
  • 2,591
  • 2
  • 14
  • 17
0

You can create it like this:

for(var i=5; i<=20; i+=5)

Read it like this:

  • Starting at a value 5 for i
  • Until i reaches 20
  • And increasing i by 5 for every step
Aember
  • 124
  • 1
  • 5
0
for ( i=5; i <= 100; i += 5 )

Or:

var j;
for ( i=1; i <= 100; i += 1 ) {
  j = i * 5;
  // ...
}