Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

257
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda