For loop with if, else, and else if loops


#1


I am not getting any error messages. But when i enter "hey" or anything else, the console prints out "that's great". It should print out "wazzup m8" if I respond to the prompt with "hey".


for(var i=0; i<3; i++){
    if(i<1){
        console.log("dank");
    }
    
    else if(i<2){
        console.log("danker");
    }
    
    else{
        var y=prompt("why hello");
        if(y="sah dude"){
            console.log("that's great");
        }
        
        else if(y="hey"){
            console.log("wazzup m8");
        }
        
        else{
            console.log("It's time to stop");
         }
    }
}


#2

The problem is that you use = instead of ==. Meaning you assign a value instead of comparing two values. So the condition is always "sah dude" no matter what you entered and as non-empty strings are treated as true you'll always get the "that's great" message.


#3

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