How to make a prompt conditional


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?


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 {

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


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:


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.


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