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

265
Vistas
How to stop loop if only one of these isn't true?

I have a while function that will run in auto mode if auto mode is activated (checkBox.checked)

The problem is this code only stops once both a and b are greater than my game limit # (bestof.value). I want it to stop once only one of these is not true.

When I use while(a || b < bestof.value) it times out until the stack reaches its limit. It also returns no values.

if ( checkBox.checked == true ) {
    while( a && b < bestof.value ) {
       myFunction();
    }
};

Any idea how I can solve this?

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

0

Are you trying to say that a and b are supposed to be smaller than bestof.value?

Unfortunately that is not how the syntax works, the && seperates statements, so basically you are saying while a is true and b is smaler than...

What you need is this:

if (checkBox.checked == true){
    while(a < bestof.value && b < bestof.value){
    myFunction();
};

As you realized correctly, your code only stops as soon a and b are above the value, since it checks if a exists and b is over the value, so basically your trigger is when b surpasses the value.

Another example:

let a = 1
let b
if (a) {
  console.log("a exists")
}
if (b) {
  console.log("b exists")
}

As you can see "b exists" is not being printed, and this is basically what you ask ur while loop before the &&, if a exists ...

about 4 years ago · Juan Pablo Isaza Denunciar

0

Mistakes you made:

  1. while condition for "I want loop to stop once one of a or b become greater than my game limit" is same as "run loop till both a and b is less then limit":
while(a < bestof.value && b < bestof.value) { ... }
  1. It's unnecessary to convert/compare if condition to boolean on your own, JS will do it automatically, so this is enough:
if (checkBox.checked) { ... }
  1. You missed '}'. Always compare count of open and close brackets if your IDE/editor don't do it.
if (checkBox.checked == true){
    while(a && b < bestof.value) {
        myFunction();
//   ↑ here you forget to close while body
};

Also: You always can stop any loop with break keyword:

white(condition) {
  if (needToStop) { break; }
}

Conclusion: your code should look like this:

if (checkBox.checked) {
    while(a < bestof.value && b < bestof.value) {
        myFunction();
    }
};
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