While or For loop for a certain number of iterations

I have an array like:

[100, 99, 101, 107, 97, 85, 74,73,69,68,68,67,68,67]

I am trying to loop over it and add an if statement for numbers above 90, if that occurs more than 4 times in a row but I’m not sure how I can add a condition to only display if at least 4 times in a row?
So far I have:

var i = 0;

for (numbers[i];) {
  if ( i > 90){
     console.log("above 90 more than 4 times in a row")
}
  i++;
}

Thanks so much!

well, if you write it like it’s certainly better to use a while loop

how would you check if a number and the following three are all higher than 90, if you had the array, and pen and paper only at yor disposal?

1 Like

Hmm good question, I guess I’d put a check next to the number and if I got one that wasn’t I put an x and continue going until I got 3 checks in a row.

and in JavaScript, how could you implement that check?

I’m thinking a while loop:

var i = 0;

while (numbers[i]) {
  if (i > 4 & numbers[i] > 90 {
  console.log("above 90 more than 4 times in a row")
  }
  i++;
}

you can certainly do that with a while loop, but i is the index in the array, it can’t also be your checkmark

2 Likes