I keep getting default answer for some reason


#1

var user = prompt("whats you favorite scary movie?").toUpperCase();
switch(user){
case 'Halloween':
if('Halloweem'&&'H20'){
console.log("ooh Michael Myers!");
}else{
console.log("H20 sucks")
}
break;
case 'night of the living dead':
if('night of the living dead'||'walking dead'){
}
else{
console.log("more brains!")
}
console.log("zombies!");
break;
case 'chucky':
console.log("dolls are scary");
break;
default:
console.log("thats not scary");
}


#2

It has to do with the string manipulation being done on the prompt() return value.

.toUpperCase()

turns all characters into capitals,

halloween => HALLOWEEN

so the cases need to match all upper case text expressions.

Personally, I recommend against this practice and favor the more reliable approach of testing the return value before applying a string method. Not all returns are strings, which will throw a fatal error when a method is called on them. Try clicking Cancel or press the Esc key.

var user = prompt(" ... ").toUpperCase();

The compiler will throw an error with something like,

TypeError: Cannot read property 'toUpperCase' of null

If we respect this as a user's wish to exit the program, or some loop in the program, then we always have an exit plan. prompt() used in a loop without an exit plan can be a pain since we lose the page.