Need help with "else if" statements (Code Your Own Adventure 2)


#1


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


Even though it says I can move on to the next lesson, I'm not satisfied with one part of this code.
I've quarantined the problem with two small comments and one big comment underneath one of the small comments.
Basically the problem is the string "Two Positives" is not showing. Instead the console only logs "Well at least one Yes". I swear my answer to both prompts badFun and goodFun, is "YES". That is two yeses, yet the console doesn't log "Two positives", even though both badFun and goodFun should be storing the value "YES".

TLDR: Once I run the program, I type FUN, then I type YES, then I type YES. But the console only logs "Well at least one yes". When really, the console should be logging "Two positives"


var user = prompt("What is your goal?").toUpperCase();


switch(user) {

case ('ASK ME NEXT TIME'):
    console.log("Okay, I will ask you later");
    break;
    
case 'MONEY': 
    console.log("Have fun with all your money"); 
    break;

case 'WISDOM':
    console.log("Have fun with all your wisdom");
    break;
    

case 'FUN':
    
     var badFun = prompt("bad fun?").toUpperCase();
    var goodFun = prompt("good fun?").toUpperCase();
    
    if( (badFun === "YES") || (goodFun === "YES") ) { 
        console.log("Well at least one yes");
    } 
    else if (badFun && goodFun === "NO") { 
        console.log("Two noes? Yikes");
    }

//problem 

    else if (badFun && goodFun === "YES") { 
        console.log("Two positives");
    }
  
//problem  

/*the "two positives" string, isn't working 
for some reason, even after answering both prompts(good
and bad fun?, with YES, the console logs "Well at least one yes.
All the other code is working just fine.. Maybe it's because I have two consecutive 'else if' statements?
How do I fix this code so that when my answer to both prompts(bad and good fun) is yes, the console logs "Two Positives". Because right now it's logging well at least one yes
*/    

else { console.log("Type something I can read please");
}
break;
    

    
default:
console.log("I don't understand what you mean");
break;
}


#2

you need to do comparison at both sides of the && operator:

if (string1 == "something" && string2 == "something else")

if you don't, the left hand side simply check if the badFun (which contains a string) is true, strings are considered true. You do it right here:

if( (badFun === "YES") || (goodFun === "YES") )

you compare at both sides, not sure why it goes wrong at this line:

else if (badFun && goodFun === "YES")

#3

If that is the case, then why does "Two noes? yikes" log to the console. It's using the exact same code except for "YES" it's checking for "NO".


#4

because the condition is always true? you don't do comparison at the left hand side of your and operator, so it looks if badFun is true, which it is. It is not compared with anything. You should compare at both side of the and operator, just like you did with the or operator


#5

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