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

256
Views
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 answers
Answer question

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 Report

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 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!