1

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
  • 204,283
  • 31
  • 260
  • 272
jakey_dev
  • 223
  • 2
  • 11

1 Answers1

5

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
  • 32,854
  • 11
  • 73
  • 106