Infinite Loop Crashing Browser


#1


https://www.codecademy.com/courses/javascript-beginner-en-ASGIv/2/4?curriculum_id=506324b3a7dffd00020bf661


Hi folks!

I'm super new to JS, and I'm missing something obvious here.. Does anyone know why this is crashing my browser?


var myName="Ignacio";
var myColor="green";


for (i=0; i<11; i++) {
    console.log("My name is" + " " + myName + " " + "and my number is" + " " + i + ".");
}

while(myColor="green") {
    console.log(myName + "'s" + " " + "favorite color is green.");
    myColor="purple";
}

var myFunction=function(x) {
    do {
        console.log("His single His Single is Dropping is dropping.") 
        } while(x=300); 
}
        
        myFunction(23);


#2

In your while loop and your do while loop you are using only one = which is used as an assignment operator. You should use a comparison operator of either == or ===


#3

while(myColor="green") {

// try to use boolean values(true or false) in your condition to easily stop a loop after it runs by changing the variable value. Assign true to myName, before the loop and assign false after the console.log to see the effect.

console.log(myName + "'s" + " " + "favorite color is green.");
myColor="purple";
}

See my comment and you may also wish to go over this:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration


#4

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