Logical Operators


#1

Could someone remind me what && and || means again? I can't find an earlier lesson with it

Cheers


#2

&& means and.
|| means or.


#3

&& means AND
and || means OR


#4

var user = prompt("A wild pikachu appears, do you want to battle? (yes/no/maybe?").toUpperCase();
console.log (user);
var attack = prompt("What do you do? Attack / Pokeball (the pikachu wants to fight)").toUpperCase();
console.log (attack);
switch(user) {
case 'YES':
if (user === "YES" && attack === "POKEBALL") {
console.log ("You catch the pikachu");
}
else
{
console.log ("You kill the pikachu");}
break;
case 'NO':
if (user ==="NO" && attack === "POKEBALL") {
console.log ("The pikachu swears at you");
}
else
{
console.log ("The pikachu kills you!");}
break;
case 'MAYBE':
if (user ==="MAYBE" || attack ==="POKEBALL")
{
console.log ("THE PIKACHU EATS A CARROT");
}
else
{
console.log ("THE PIKACHU WANTS TO BE YOUR FRIEND");}
break;
default:
console.log ("I've never heard of that one.");

};

So i have this, how could i use || in this? Where i have put it, i don't think it's working. It's letting my progress through the course though, but i want to solve this


#5

The reason why people would use || is because they just want at least one of the statement to be True. && works when all statements have to be True. In your case, the user has to input "MAYBE" or "POKEBALL".

They can input "YES" and "POKEBALL" and this statement would be prompted since one of the statements became True (which is the attack === "POKEBALL").

You can put

case 'MAYBE':
if (user ==="MAYBE" || attack ==="ATTACK") { //make POKEBALL somthing else
    console.log ("THE PIKACHU EATS A CARROT");
} else {
    console.log ("THE PIKACHU WANTS TO BE YOUR FRIEND");
}
break;

#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.