5. Logical Operators - Code your own adventure 2


#1

The following code shows a case inside a switch statement.

case 'red':
var machine = prompt("You suddenly gasp as you find yourself in a pod filled with cold liquid, A sinister-looking machine hovers towards your pod - do you play dead? (yes or no)").toLowerCase();
if (machine === 'yes') {
var next = prompt("The machine hovers nearby for a short while and then moves away.You now decide to break out of the pod. Do you punch, kick or push the pod? (punch, kick or push)").toLowerCase();
if (next === 'punch' || 'kick') {
console.log("The machine returns and suddenly drives a metal stake through the pod and into your body - you die");
}
else {
console.log("The pod opens and you manage to escape");
}
}
else {
console.log ("The machine opens the pod and before it is able to do anything else, you jump out and plunge into the water below");
}
break;

Sorry I don't know why the spacing isn't being carried over.

My problem is that when the prompt asks whether the user wants to punch, kick or push the pod, any input logs "The machine returns and suddenly drives a metal stake through the pod and into your body - you die" which is only supposed to be logged when the user inputs "punch" or "kick".

Thank you in advance!


#2

it has to do with the order in which the logical operators are dealt with.
on the line: if (next === 'punch' || 'kick') ..... you should put brackets around the OR statement like this: if (next === ('punch' || 'kick')) which causes the OR condition to be read as a whole and then tested to the value of your next function
I tested it and it worked for me. Hope it does for you as well


#3

Did you really test it properly? I tested it and it didn't work. Because you can't just wrap several answers in parentheses like you did.

if (next === ('punch' || 'kick')) // not working properly

The if-condition might work with the answer "punch" but it doesn't work with "kick". You need to write a proper condition:

if (next === 'punch' || next === 'kick')


#4

@pyninja00741 @schamanu

Thanks for the help! So I have tried the following changes in the code and get the following output:

If (next === ‘punch’ || ‘kick’)
punch - The machine returns and suddenly drives a metal stake through the pod and into your body - you die (desired output)

kick - The machine returns and suddenly drives a metal stake through the pod and into your body - you die (desired output)

push - The machine returns and suddenly drives a metal stake through the pod and into your body - you die (undesired output)

if (next === ('punch' || 'kick'))
punch - The machine returns and suddenly drives a metal stake through the pod and into your body - you die (desired output)

kick - The pod opens and you manage to escape (undesired output)

push - The pod opens and you manage to escape (desired output)

if (next === 'punch' || next === 'kick')
punch - The machine returns and suddenly drives a metal stake through the pod and into your body - you die (desired output)

kick - The machine returns and suddenly drives a metal stake through the pod and into your body - you die (desired output)

push - The pod opens and you manage to escape (desired output)

-

It looks like the code if (next === 'punch' || next === 'kick') does the job but I dont understand how the changes in syntax in if (next === ‘punch’ || ‘kick’) and if (next === ('punch' || 'kick')) causes their respective changes in output. Can anyone explain to me how the computer interprets the syntax in this context?

Cheers


#5

I can't really explain how

if (next === ('punch' || 'kick'))

works because I have never seen anyone write a condition like that before. Obviously it's wrong. I assume the condition can never be true because the variable "next" can only store 1 value. Either it stores the value "punch" or the value "kick". It can't store both at the same time.

The condition

if (next === ‘punch’ || ‘kick’)

however is pretty straightforward. First you have to know that the condition is separated into 2 parts by the logical OR "||". The first part, next==='punch' is a simple comparison. The computer checks if this condition is fulfilled or not. Then it goes on to the next part. In this 2nd part ( the string 'kick') we don't have a comparison. We only have 1 value. So you could say it can't be evaluated and it should show an error message. You could also say that when there is no comparison the computer should always evaluate this string as 'false'. Or you could say that the computer should always evaluate a single value as true. The developers of javascript chose the latter. In javascript a string that is not compared to anything will just be evaluated as true.

Now we need to distinguish between conditions that use the logical OR ('||') and conditions that use the logical AND (&&). If you combine 2 comparisons with && to a single condition than both comparisons have to be TRUE for the whole condition to be evaluated as TRUE. If only 1 of the comparisons (or even both) are FALSE, then the whole condition will be FALSE as well.

If you use || on the other hand, a condition consisting of multiple comparisons ( easiest case would be 2 comparisons) will be TRUE if AT LEAST 1 of the comparisons is true.

The problem with your condition

If (next === ‘punch’ || ‘kick’)

was that 'kick' (2nd part of condition) is always evaluated as TRUE. And because you combine this part by a logical OR the whole condition is always TRUE because one part of it is always TRUE. Therefore you get wrong results with this condition.