Stopping a loop once a case is used


#1


https://www.codecademy.com/en/courses/javascript-beginner-en-ZA2rb/0/5?curriculum_id=506324b3a7dffd00020bf661


No error message but I'm trying to loop the switch until a valid input is recognized. Using a do, while loop. It works but after the function is complete it loops back to the start again. How to stop the loop once there are valid inputs?


var wrapper = function(){
    do {
var user = prompt("Which one is most important? (Choose LOVE, MONEY or HAPPINESS").toUpperCase();
switch(user){
    case "LOVE":
        var l1 = prompt("Is it better to have love and lost or not to have loved at all? (Choose LOVE or LOST).").toUpperCase();
        var l2 = prompt("Is it true all we need is love? (Choose YES or NO)").toUpperCase();;
        if(l1 === "LOVE" && l2 === "YES"){
            console.log("You're a true believer. Well done, you.");
        } else if (l1 === "LOVE" || l2 === "YES"){
            console.log("You're a part believer.")
        } else { 
            console.log("You have no illusions")
        }
        break;
    case "MONEY":
        var m1 = prompt("Can money buy happiness? (Choose YES or NO)").toUpperCase();
        var m2 = prompt("Is it all about the money money? (Choose YES or NO").toUpperCase();
        if(m1 === "YES" && m2 === "YES"){
            console.log("You must really like money.");
        } else if (m1 === "YES" || m2 === "NO"){
            console.log("It's not about the money money. We don't need your money money")
        } else { 
            console.log("You're goddamn right!")
        }
        break;
    case "HAPPINESS":
        var h1 = prompt("Do you achieve happiness with people or things? (Choose PEOPLE or THINGS)").toUpperCase();
        var h2 = prompt("Are these interchangable? (Choose YES or NO)").toUpperCase();
        if(h1 === "PEOPLE" && h2 === "YES"){
            console.log("Wow.");
        } else if (h1 === "PEOPLE" || h2 === "NO"){
            console.log("How loyal. Very good.")
        } else { 
            console.log("You should probably go buy some stuff then.")
        }
        break;
    default:
    console.log("That wasn´t one of the options, try again.");
}
}
while(user!="LOVE"||"MONEY"||"HAPPINESS")
}
wrapper()


#2

make a valid while condition? Your condition is wrong. You need to compare after each or operator:

while(user!="LOVE"||user != "MONEY"|| user != "HAPPINESS")

and this one is still not valid. you have to use && instead of ||. Think about this one for a second.

first, the loop keeps running as long as the condition evaluates to true. If the user enters something invalid, all conditions are true, so the loop keeps running. If the user enters something valid (for example MONEY), we have: true || false || true

true or false is true, so the loop keeps running. Which is why you should &&. the moment the user enters something invalid, all conditions are true, the loop keeps running. the moment the user enters, for example MONEY) you get:

true && false && true

which is false, the loop ends as it should, because the user enters something valid.


#3

That makes absolute sense..
Thank you for taking the time to explain.