0

I'm trying to run a piece of code every 7th iteration inside my for loop.

How would I do this?

for (let i = 1; i < 101; i++) {
  if (i == 7) {
    console.log('7 iterations have passed')
  }

  console.log(i)
}

Currently it only does it once, I'm not sure what's the clean way of checking this.

j08691
  • 197,815
  • 30
  • 248
  • 265
jakey_dev
  • 185
  • 8

1 Answers1

4

With the modulo operator

for (let i = 1; i < 101; i++) {
    if (i % 7 === 0) {
        console.log(i +' iterations have passed')
    }

    console.log(i)
}
yunzen
  • 31,553
  • 11
  • 69
  • 98