Only "if" statements log to console, never "else if" or "else"


#1


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


This code will pass & let me go to the next lesson, but it is not behaving properly. Although the "action" variable will take me to the correct case, it seems that the "fear" variable &/or the variables inside the cases are being ignored. No matter how i answer the prompt questions inside the cases, only the "if " statement ever logs to the console (never the "else if" or "else" statements). What am I missing?


var fear = prompt("You are walking down a dark alley when a thug approaches you demanding money. Are you scared?").toLowerCase();

var action = prompt("Do you 'pay', 'fight' or 'run'?").toLowerCase();

switch(action) {
    case "pay":
        var money = prompt("Do you have any money?").toLowerCase();
        if ((fear == "yes" || "y") && (money == "yes" || "y")) {    // fear yes, money yes
            console.log("Sensing your fear, the thug accepts your money, but beats you to a bloody pulp anyways. Never show fear!");
        } else if ((fear == "yes" || "y") && (money == "no" || "n")) {  // fear yes, money no
            console.log("Sensing your fear & being angry about wasting his time for no money, the thug beats you to a bloody pulp. Never show fear!");
        } else {    // fear no
            console.log("Sensing that you have no fear & fearing for his own safety, the thug runs away!");
        }
        break;
    case "fight":
        var skills = prompt("Do you have ninja skills?").toLowerCase();
        if ((fear == "yes" || "y") && (skills == "yes" || "y")) {   // fear yes, skills yes
            console.log("Sensing your fear, the thug jumps you, but you manage to fight him off & live to see another day.");
        } else if ((fear == "yes" || "y") && (skills == "no" || "n")) { // fear yes, skills no
            console.log("Sensing your fear, the thug jumps you & beats you to a bloody pulp. Never show fear!");
        } else {    // fear no
            console.log("Sensing that you have no fear & fearing for his own safety, the thug runs away!");
        }
        break;
    case "run":
        var speed = prompt("Are you fast?").toLowerCase();
        if ((fear == "yes" || "y") && (speed == "yes" || "y")) {    // fear yes, speed yes
            console.log("Sensing your fear, the thug lunges at you, but you manage to outrun him & live to see another day.");
        } else if ((fear == "yes" || "y") && (speed == "no" || "n")) {  // fear yes, speed no
            console.log("Sensing your fear, the thug lunges at you. You can't outrun him so he beats you to a bloody pulp. Never show fear!");
        } else {    // fear no
            console.log("Sensing that you have no fear & fearing for his own safety, the thug runs away!");
        }
        break;
    default:
        console.log(action + " is not an option!");
};


#2

What does this print?

console.log("purple" === 5 || "purple");

#3

switch(action) {
    case "pay":
        var money = prompt("Do you have any money?").toLowerCase();
        if (fear == "yes" && money == "yes") {    // fear yes, money yes
            console.log("Sensing your fear, the thug accepts your money, but beats you to a bloody pulp anyways. Never show fear!");
        } else if (fear == "yes" && money == "no") {  // fear yes, money no
            console.log("Sensing your fear & being angry about wasting his time for no money, the thug beats you to a bloody pulp. Never show fear!");
        } else {    // fear no
            console.log("Sensing that you have no fear & fearing for his own safety, the thug runs away!");
        }
        break;

This example works for the first case. The problem with your code was the addition of || in your if statements. I have been trying to find a way to use both "y" and "yes" for acceptable answers, but I am also having difficulty. Save for creating multiple if statements, I can't find a suitable replacement. Will update you with results or if anyone can offer assistance it would be appreciated!


#4

 if ((fear == "yes" || "y") && (money == "yes" || "y"))  //wrong

if ((fear == "yes" || fear =="y") && (money == "yes" || money == "y"))  //right

same for all. make sense :slight_smile:


#5

Thanks, @rcodeman! Yes, it makes sense. I'm not really sure why I thought i could do it that way. Just out of curiosity, it there any shorthand available instead of writing out fear == "x" || fear == "y" || fear == "z" ?


#6

It prints purple . I'm not really sure what you're getting at, though.


#7

You use similar expressions all over your code. What do you think they evaluate to? Compare your condition expressions to my expression, if my expression evaluates to "purple", then what do your expressions evaluate to? What should boolean expressions evaluate to in order to be useful?

@rcodeman skipped ahead to the conclusion of what I'm pointing out.


#8

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