How to make a prompt conditional


#1

https://www.codecademy.com/en/courses/javascript-beginner-en-qDwp0/1/4?curriculum_id=506324b3a7dffd00020bf661


Hi friends! I'm trying to have a little bit of fun with this switch statement exercise. I wanted to create another prompt for certain responses to the first prompt, e.g.:

"Which type of coffee do you prefer?"
user: "americano"
"Mmm...Hot or iced?"
user: "hot"
"In heat like this??"

First of all, the browser is reading the code as calling both prompts right away, when I only want the second prompt to happen in response to select cases. Secondly, the tempResponse function is only returning the response to the first option, no matter what answer the user inputs.

Can you tell me how to fix this?

Thanks!


var userPreference=prompt("Which type of coffee do you prefer?");

var temperaturePreference=prompt("Mmm...Hot or iced?");
var tempResponse=function(temp) {
    if(temp==="hot") {
        console.log("In heat like this??");
    } else if(temp==="iced") {
        console.log("Good choice! A little basic, but hey..");
    } else {
        console.log("..gross");
    }
};

switch(userPreference) {
    case "black":
        console.log("Like your ladies, eh?");
        break;
    case "latte":
        tempResponse(temperaturePreference);
        break;
    case "americano":
        tempResponse(temperaturePreference);
        break;
    case "flat white":
        console.log("I hope you like 'em tall..");
        break;
    case "frappuccino":
        console.log("I'm just..embarrassed for you.");
        break;
    default:
    console.log("...If that's what makes you happy!")
};


#2

Also, I'm not a racist or a misogynist! -- I'm white and my girlfriend is black, and I wrote those responses to make her laugh. I didn't remember all of the case responses I had written until I posted this :slight_smile:


#3

Both of your prompts follow directly after each other so they are going to be presented to the user one after the other. If you want the second one to happen at a different stage in your program then move it to where you'd like it to be.

I couldn't replicate your second problem with what your tempResponse function is logging.


#4

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