Adventure 2 Logical Operators


#1

I am very confused... I have put a lot into this code and i am not sure if it works, i got an error saying telling me 'unexpected token case' and i was hoping someone or some people could tell me whats wrong, why it's wrong, and how the code correctly operates, here is my code:

var user = prompt ("You are walking through a neighbor hood at night and you see an old house. Dou you want to WALK IN, RUN AWAY, or CALL SOMEONE?").toLowerCase();

switch (user) {
case 'walk in':
var light = prompt ("You walk through the front door. It's pitch black. Do you want to turn your phone's flashlight on?")
if (light == yes) {
var bright = prompt ("How bright do you set the light to? (1-5)");
if (bright == "1" || "2") {
console.log("Your Light is dim, you can't see very far");
}
else if (bright == "3") {
console.log("Your light isn't very bright, but you can see clearly");
}
else if ("4" || "5") {
console.log("Your light is bright, you see a shadow on the wall");
}
else {
console.log("You decide to keep your light off, you don't want to attract any attention");
}
break;
case 'run away':
console.log("you start running, you don't want to be near that house any more")
break;
case 'call someone':
var call = prompt ("Who do you want to call? Your mom, best friend, or the police?")
if (call == mom) {
console.log("You decide to call your mom... she doesn't answer");
}
else if (call == best friend) {
console.log("You decide to call your best friend... she doesn't answer"
}
else {
console.log("You decide to call the police, maybe they can help... there is no answer from the authorities")
}
break;


#2

@magiccookie it looks like you've got a couple things going on here. The case error is because after this line

console.log("You decide to keep your light off, you don't want to attract any attention");

You need one more } to close out the if statement. On this line:

else if (call == best friend) {

You need one more equal sign for the comparison.

On this line:

console.log("You decide to call your best friend... she doesn't answer"

You need a closing parentheses at the end of the string. Then at the end, you need a closing curly brace after the final break. Once you do all that, your code should run, though you'll probably want to put quotes around your strings that are being used for comparisons (best friend, light, etc.).

See how it works once you makes those changes. You should be good!


#3

thanks so much! i was afraid the code wouldn't run, so glad it does!


#4

@freginold @magiccookie "==" can be used for comparisons. But you are right, it is safer to use "===" because then not only the value but also the data type have to match.
e.g. if you compare the string "7" with the number 7 "==" will show that they are equal. But comparing the string "7" with "===" with the number 7 will result in FALSE, because a string is not the same as a number.

Furthermore in this condition

else if (call == best friend) {

you need quotes around the value "best friend". Otherwise javascript interpretes best friend as variable name, but variable names are not allowed to consist of 2 words.


#5

ok thanks, I just got confused and forgot to add the quotes, and I was using === but the teacher thing to the right kept using == so I thought they were interchange able (even though they said they weren't), but thanks for clearing that up for me!


#6

@schamanu thanks, I didn't know that about the ==.