0

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?

Mojo Allmighty
  • 793
  • 7
  • 18
  • 3
    `while (a < bestof.value && b < bestof.value)`. Javascript doesn't work like English. – deceze Dec 07 '21 at 14:53
  • 1
    _"once both a and b are greater than my game limit"_ - That's not correct. The loop will end when `a` is [truthy](https://developer.mozilla.org/en-US/docs/Glossary/Truthy) and `b` is equal or greater than `bestof.value` – Andreas Dec 07 '21 at 14:54
  • Does this answer your question? [JavaScript: Simple way to check if variable is equal to two or more values?](https://stackoverflow.com/questions/12116326/javascript-simple-way-to-check-if-variable-is-equal-to-two-or-more-values) – Heretic Monkey Dec 07 '21 at 14:55
  • Note that you have mismatched brackets (two opening curly brackets, at the end of the `if`, and at the end of the `while`, and one closing curly bracket). – Heretic Monkey Dec 07 '21 at 15:05

2 Answers2

1

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

Branchverse
  • 1,203
  • 1
  • 7
  • 19
  • the 2 win values (a and b) should both < game limit #, and if one is false then loop ends. I'm pretty new to JS so excuse the ignorance. – JacobVerDugt Dec 07 '21 at 15:44
  • One who invests time to learn something new is not ignorant no matter the stage of the learning process – Branchverse Dec 07 '21 at 15:46
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();
    }
};
Xeelley
  • 1,081
  • 2
  • 8
  • 18