Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop in JavaScript until a condition is met

I'm trying to loop indefinitely until a condition is met...is the following correct?

It seems not to be.

    var set = false;
    while(set !== true) {
        var check = searchArray(checkResult, number);
        if(check === false) {
            grid.push(number);
            set = true;
        } 
    }
like image 481
Funktion Avatar asked Jan 26 '16 11:01

Funktion


2 Answers

Basically, you can make an infinite loop with this pattern and add a break condition anywhere in the loop with the statement break:

while (true) {
    // ...
    if (breakCondition) {            
        break;
    } 
}
like image 155
Nina Scholz Avatar answered Oct 23 '22 07:10

Nina Scholz


The code will loop while searchArray result is not false and until it becomes false. So the code is correct if you wanted to achieve such behavior and it's not correct otherwise.

like image 22
MobileX Avatar answered Oct 23 '22 06:10

MobileX