Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

119
Views
Using Continue in a While loop in Javascript

Please help me to find out my mistake(s) in this code.

var  numbers = [12, 34, 23, 98, 08, 78, 73, 48];
var i = 0;

while (i < numbers.length){
    if (numbers[i] >70){
        continue;
    }
    console.log(numbers[i])
    i++;
}

I want to get output

12
34
23
8 
48

But the code shows output:

12
34
23

and the run doesn't stop.

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You have an infinite loop, because if numbers[i] > 70 it's not increasing i, so the next loop it's checking that same condition again without an incremented i value. There are multiple ways to fix this, but one option is to output the value when numbers[i] <= 70, and always increment i.

var numbers = [12, 34, 23, 98, 08, 78, 73, 48];
var i = 0;

while (i < numbers.length){
    if (numbers[i] <= 70){
        console.log(numbers[i]);
    }
    
    i++;
}

Another option would be a different loop:

var numbers = [12, 34, 23, 98, 08, 78, 73, 48];
var i = 0;

for(i = 0; i < numbers.length; i++) {
    if (numbers[i] > 70){
       continue;
    }
    
    console.log(numbers[i]);
}

Or a simpler way:

var numbers = [12, 34, 23, 98, 08, 78, 73, 48];
var i = 0;

for(i = 0; i < numbers.length; i++) {
    if (numbers[i] <= 70){
        console.log(numbers[i]);
    }
}

about 4 years ago · Juan Pablo Isaza Report

0

You need to update i value before using continue if you don't the value of i will remain the same which leads to the loop becoming an infinite loop.

var  numbers = [12, 34, 23, 98, 08, 78, 73, 48];
var i = 0;

while (i < numbers.length){
    if (numbers[i] >70){
        i++;
        continue;
    }
    console.log(numbers[i])
    i++;
}

about 4 years ago · Juan Pablo Isaza Report

0

Instead of interrupting the while loop you can re-write your logic as:

  if (numbers[i] < 71){
    console.log(numbers[i]);
  }

so that the default action on each iteration of the loop is simply to increment i and, separately, the console logs a value only if the condition is satisfied.


Working Example:

let numbers = [12, 34, 23, 98, 08, 78, 73, 48];
let i = 0;

while (i < numbers.length){

  if (numbers[i] < 71){
    console.log(numbers[i]);
  }
  
  i++;
}

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!