M.C. M.C. - 22 days ago 11
Javascript Question

Javascript - inc var +18 each certain value stepper


  • Hi, fast and simple: simple JS

  • this code work 100% - increment +1 each single For step: OK!

    - I NEED increment +1 each 3 steps.



    var x = 0;

    for(var _i = 0; _i < 2000; _i++)
    {
    x++; // --- OK
    x = x + 18; // --- "each _i=3, _i=6, _i=9, _i=12..." (etc)
    };



- how?




  • thanks in advance
    enter code here


Answer

Wouldn't modulo work for you here.

var x = 0;
var maxLimit = 20;
for (var _i = 0; _i < maxLimit; _i++) {
  x++; // --- OK
  if (_i % 3 === 0 && _i > 0) {
    x = x + 18; // --- "each _i=3, _i=6, _i=9, _i=12..." (etc)
    console.log('Increment: x + 18...');
    console.log('_i = ', _i);
    console.log('x = ', x);
  }
}