Whyyyyyyy gives me infinite loop


#1


Just does'nt log and always gives me an infinite loop...my mouse is broken from hitting refresh so many times


var x = true;
var soloLoop = function(){
while(x = true){
        console.log("Looped once!");
            x = false;
            }
};

soloLoop();


#2

Comparisons use ===, not the assignment operator, =. In this case, however, x is a boolean literal and all we need to do is test the truthiness.

while (x) {

}

is the same as, while (x === true) {}


#3

thnx.. another quest. tho, what does the function do in all of this?


#4

The function is a basic example of a re-usable block of code, which is what a function is. Just as a loop contains a block that can be run any number of times, a function can be called any number of times from anywhere in the namespace.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.