• basdiljhs@lemmy.world
    link
    fedilink
    arrow-up
    2
    ·
    7 months ago

    for(var i=0;i<=100;i++){ if((i%2)==1) console.log(i); }

    btw % is the modulo operator, x%y returns the remainder of division of x by y

    • LostXOR@fedia.io
      link
      fedilink
      arrow-up
      1
      arrow-down
      1
      ·
      7 months ago

      Slightly simpler, start at 1 and increment by 2 so you don’t have to check whether i is odd.

      for (var i = 1; i < 100; i += 2) {
        console.log(i);
      }