5. Logical operators: always default outcome?


#1

var location = prompt ("Where do you wish to go (FANTASYLAND, WARRIOR KINGDOM, or PEACE MOUNTAIN)?").toUpperCase();
var skill = prompt ("What is your specialty (MAGIC, FIGHTING, or HEALING)?").toUpperCase();
var bravery = prompt ("Are you willing to fight to the end?").toUpperCase();
switch(location) {
case 'FANTASYLAND':
if(skill === 'MAGIC' || bravery === 'YES') {
console.log("You are destined to live there forever!");
} else {
console.log("Are you sure you are ready for this journey?");
}
break;
case 'WARRIOR KINGDOM':
if(skill === 'FIGHTING' && bravery === 'YES' ) {
console.log("You stand the greatest chance of surviving the journey!");
} else {
console.log("This journey may not fair you well.");
}
break;
case 'PEACE MOUNTAIN':
if(bravery === 'NO' && skill === 'HEALING') {
console.log("This is your destined home.");
} else {
console.log("You may easily become bored here");
}
break;
default:
console.log("Your land and skills are new to my but surely strong.");
};


#2

Looks fine. When you enter your answer in the prompt, make sure you don't enter any spaces, and there are no typos.


#3

I am not super familiar with switch statements, but maybe there should be a 'break;' after the default?


#4

I tried it but I still get the default every time with all caps, no spaces, even took spaces out of the code but only the default comes up. Thank you for the help by the way.


#5

@dlnky I believe I found the problem!

var location = prompt ("Where do you wish to go (FANTASYLAND, WARRIOR KINGDOM, or PEACE MOUNTAIN)?").toUpperCase();

in the code above, there should be a \ before the parentheses inside the string, like so:

var location = prompt ("Where do you wish to go \(FANTASYLAND, WARRIOR KINGDOM, or PEACE MOUNTAIN\)?").toUpperCase();

And do so for the rest of the parentheses inside your strings so JS doesn't think you're ending the prompt early. Make sense?


#6

@dlnky Oh! Silly me! Also, make sure to put your separate conditions in separate parentheses in your if statement, like so:

if ((skill === 'FIGHTING') && (bravery === 'YES'))

rather than,

if(skill === 'FIGHTING' && bravery === 'YES' )

I think that might have something to do with it. Excuse me if I'm wrong. It's been a while since I've dealt with && and ||.


#7

I'm gonna try it right now thank you so much!


#8

Still getting the default. :confused:


#9

@dlnky Do you mind posting the new, edited code?


#10

var location = prompt ("Where do you wish to go (FANTASYLAND, WARRIOR KINGDOM, or PEACE MOUNTAIN)?").toUpperCase();
var skill = prompt ("What is your specialty (MAGIC, FIGHTING, or HEALING)?").toUpperCase();
var bravery = prompt ("Are you willing to fight to the end?").toUpperCase();
switch(location) {
case 'FANTASYLAND':
if((skill ==='MAGIC') || (bravery ==='YES')) {
console.log("You are destined to live there forever!");
} else {
console.log("Are you sure you are ready for this journey?");
}
break;
case 'WARRIOR KINGDOM':
if((skill === 'FIGHTING') && (bravery === 'YES')) {
console.log("You stand the greatest chance of surviving the journey!");
} else {
console.log("This journey may not fair you well.");
}
break;
case 'PEACE MOUNTAIN':
if((bravery ==='NO') && (skill ==='HEALING')) {
console.log("This is your destined home.");
} else {
console.log("You may easily become bored here");
}
break;
default:
console.log("Your land and skills are new to my but surely strong.");
break;
};


#11

I've spotted your problem, and it's a sneaky one!

'location' is a reserved word in Javascript, meaning it can't be used for variables, labels or function names.

If you change all references to 'location' your code should run fine.

As a side note, the default case doesn't need a break, as it's the final case listed (although it'll still run normally).


#12

Wow, I had no idea thanks man!