Successful Example of Nesting if Inside Switches


#1


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


I just wanted to share an example of code that works.


var userShirt = prompt("What color is your shirt today?","say blue, tan, or white").toUpperCase()

switch (userShirt){
    case 'BLUE':
        var userEyes = prompt("What color are your eyes?","say blue, brown, or hazel").toUpperCase()
        if(userEyes === 'BROWN' || userEyes === 'HAZEL'){
            console.log("Your eyes don't match your shirt.");
        } else {
            console.log("Your eyes match your shirt!");
        }
        break;
    case 'TAN':
        var userWatch = prompt("Do you have a watch on?","say yes or no").toUpperCase()
        if(userShirt === 'TAN' && userWatch === 'YES'){
            console.log("Cool--your eyes match your watch!");
        } else {
            console.log("I guess you don't mind being late...");
        }
        break;    
    case 'WHITE':
        var userCap = prompt("Do you have a baseball cap on?","say yes or no").toUpperCase()
        if("NO"){
            console.log("Dude--a white shirt and a cap is the move!");
        } else {
            console.log("Rock on--that's a great combo!");
        }
        break;
    default:
        console.log("Hrm... we're a bit lost here!")
};


#2

Er if i'm not mistaken, we're not supposed to share answers, or rather give answers directly to help people get through the lesson, as it defeats the purpose of having the lessons in the first place.


#3

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