Code your own adventure - switch 4


#1

Can someone help me with this switch. When I enter any of the choices, it goes straight to the default answer instead of printing that choice.

var user = prompt("Are you ready to go on your quest?!").toUpperCase();

switch(user){
case 'breakfast':
console.log("Great! Let what we can find for breakfast?");
break;
case 'lunch':
console.log("Great! We're all hungry be now. Lets go!");
break;
case 'dinner':
console.log("Great! Dinner is always exciting! Lets go!");
break;
case 'bakery':
console.log("Great! Let go find some sweets!");
break;
case 'cafe':
console.log("Great! Let find a good cup of coffee!");
break;
default:
console.log("Let go on the a quest! What are you craving?");
}


#2

You're using .toUpperCase() but all your case conditions are lower case, that's why...


#3

So if I add lowercase to all the other choice, would that make my code run correctly?


#4

It sure will,

the problem is that no matter what the use types the string would then be transformed to uppercase and then since it didn't match any case conditions you had it defaulted.

You can either use to.Lower.Case() method or you can change all your conditions to be UPPERCASE strings.


#5

I'd revised my code and it seem there something wrong..I'm I missing something now?

var user = prompt("Are you ready to go on your quest?!").toUpperCase();

switch(user){
case 'breakfast':
console.log("First meal of the day!").toUpperCase();
break;
case 'lunch':
console.log("We're all hungry be now. Lets go!").toUpperCase();
break;
case 'dinner':
console.log("Dinner is always exciting!").toUpperCase();
break;
case 'bakery';
console.log("Let go find some sweets!").toUpperCase();
break;
case 'cafe':
console.log("Let find a good cup of coffee!").toUpperCase();
break;
default:
console.log("Let go on a quest! What are you craving?").toUpperCase();
}


#6

you can't use the method toUpperCase() that way..

either change all your conditions to uppercase, for example "lunch" to "LUNCH" in your case statements or use .toLowerCase() in your prompt instead of toUpperCase().