My OR ( | | ) logic operator isn't working like I wan't it to


#1
var pass = prompt("What's the Secret Password").toUpperCase();

switch(pass) {
    case '':
        var day = prompt("What day is it today?").toUpperCase();
        if(day === "SATURDAY" || "SUNDAY")   {
            console.log("There's a sign posted on the door that says: 'CLOSED FOR THE WEEKEND'");
        }
        else    {
            console.log("... Oh ok fine there's no password you can come in.");
        }
        break;

when I run this it always returns with "There's a sign posted on the door that says: 'CLOSED FOR THE WEEKEND'" even if day doesn't equal Saturday or Sunday.


#2

This is only a portion of my code. Here's the whole thing.

var pass = prompt("What's the Secret Password").toUpperCase();

switch(pass) {
case '':
var day = prompt("What day is it today?").toUpperCase();
if(day === "SATURDAY" || "SUNDAY") {
console.log("There's a sign posted on the door that says: 'CLOSED FOR THE WEEKEND'");
}
else {
console.log("... Oh ok fine there's no password you can come in.");
}
break;
case 'There is no password' ||'There isn\'t a password' :
console.log("You're right! Twas a trick question! Come on in.");
break;
case 'Intimidation':
var tall = prompt("Are you tall?").toUpperCase();
var scary = prompt("Are you scary?").toUpperCase();
if(tall == "YES" && scary == "YES") {
console.log("Ack! Don't hurt me!");
}
else {
console.log("You ain't so tough!");
}
break;
default:
console.log("WRONG!");
}


#3

Understanding logical expressions requires that we understand how JavaScript treats non-Boolean values and expressions. Since all conditionals yield a Boolean, JS must evaluate each term and convert it to true or false. Terms that convert to true are called truthy and those that convert to false are called falsy.

The only falsy terms are as follows:

0
""
''
null
NaN
undefined

We could include false in the list, but, it is already false so we can't call it falsy since there is no conversion taking place. It is a Boolean primitive, already.

Also, we would never write undefined in our code since it is not a JavaScript object, per se. It is a computed response of the interpreter when there is no defined return value from a function, or no defined value for a variable.

Lastly, we would never write NaN in our code, either, since JavaScript has a built-in function to test for that condition, isNaN(), and similar to undefined is returned by the interpreter when an object cannot be converted to a number.

That's the preamble, now let's look at OR. This logic operation is known as short-circuiting since it returns true on the first truthy (or true primitive) operand it encounters in the expression.

T || F || F || F  => TRUE
^
|__ short circuit

None of the other operands are examined. Only the first.

Looking at strings in a conditional expression...

"string1" || "string2"  =>  TRUE
    ^
    |__ short circuit on truthy value (not an empty string)

Strings are expressions, even when it is not obvious.

var a = "string3";
if (a === "string1" || "string2") {}  => true

Huh? It's true because even though the first exprression yields false the second yields true since it is truthy. The correct way to evaluate this would be to include the comparator in both expressions:

if (a === "string1" || a === "string2") {}  => false

Starting to make sense, now? Keep asking questions if you are still unclear on this.


#4

var a = "string3";
if (a === "string1" || "string2") {} => true

I don't understand how this works. Shouldn't the statement return false because A doesn't equal "string1" or "string2". Could you elaborate on this more?


#5

We need to treat each operand independently. The comparator does not apply to both operands, only the expression in which it appears. It is not,

if ( a === "apple" || "orange")

It is,

if (a === "apple" || a === "orange")

Study up on truthiness some more so you get what I wrote about earlier.


#6

I understand. Thanks for your help.