Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

155
Vistas
Javascript continue not working even when condition is met

The isPrime() function returns true if a number is a prime number and it returns false if not. The loop should go through 2 to 500 backward and run it in the isPrime() function. If the number is not a prime number the loop should continue to the next loop iteration. And also, for the number which is a prime number, it should output in the paragraph's textContent, but the continue is not working.

Here is the code:

    let i = 500;
    const para = document.createElement('p');

    function isPrime(num) {
      for(let i = 2; i < num; i++) {
        if(num % i === 0) {
          return false;
        }
      }

      return true;
    }


    // Add your code here
while( i >= 2 ){
 if( isPrime(i) === false){
  continue;
 } else{
  para.textContent = `${i}` + "</br>";
 }
 i--;
}

And here is the fiddle: https://jsfiddle.net/au9o2ftn/1/

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can simply do this:

let i = 500;

function isPrime(num) {
  for (let i = 2; i < num; i++) {
    if (num % i === 0) {
      return false;
    }
  }

  return true;
}


while (i >= 2) {
  if (isPrime(i)) {
    console.log(i + " is prime!");
  }
  i--;
}

without continue...

The problem is that when isPrime(i) in your original code returns true, it continue directly and didn't finish i--. As a result, it becomes a dead loop with infinitely checking isPrime(500).

Or, if you wish to keep continue, put i-- before the continue statement.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda