5. Logical Operator Trouble


#1



https://www.codecademy.com/courses/javascript-beginner-en-ZA2rb/0/5?curriculum_id=506324b3a7dffd00020bf661#


I've already passed the actual exercise but I have what seems like a very important question. If someone could offer me some answers that would be super awesome! I'm having a lot of fun with this coding hobby and want to know it all! :smiley:

Basically my only concern is within the 'february' case. When i insert the || 'or' operator into my if/else function I get some really funny reactions inside my code.

For training purposes, I've taken liberty of making February 2nd a temporary holiday. Lol.

Under the 'february' case, if I make (day === 14 || day === 2) and input "14" as my day, it doesn't give me the desired response "Happy Holiday", but instead the "Have a good February!" response.

Now if I insert (day = 14 || day ===2) into the if/when statement inside of the 'february' case, everything in my code works flawlessly.

However, if I insert (day = 14 || day = 2) I get a syntax error.

As you can see, the only changes occuring are in the "===" and "=" signs of my if/when statement.

What is going on with these === and = signs that I am missing?

If you need further explanation on what it is I'm asking for, just let me know and I'll try to word it better! :slight_smile:

Thanks ahead of time!


var month = prompt("What is the month?").toLowerCase();
var day = prompt("What is the day?").toLowerCase();

switch (month) {
    case 'january':
        if (day = 1) {
            console.log("Happy Holiday") 
        }
        else {
            console.log("Have a good January!")
        }
        break;
    case 'february':
        if (day = 14 || day === 2) {
            console.log("Happy Holiday")
        }
        else {
            console.log("Have a good February!")
        }
        break;
    case 'may':
        if (day === 30 && day === 31) {
            console.log("What dimension are you in?")
        }
        else {
            console.log("Happy May!")
        }
        break;
    default:
        console.log("I've never heard that one before!")
        break;
}


#2

I can explain it.

=== is the identical comparison operator (checks if two variables have equal values and are of the same type).

= is the assignment operator.

When you use day === 14, you are saying that day is of the same type as the number 14. Of the same type meaning it could be any numeric number.

When you use day = 14, you are assigning day to the number 14. So, if day is assigned to the number 14, whatever is inside the statement will run true.

I hope this helps!

Kat


#3

That's actually super helpful, thank you so much! Now I totally understand the distinction between the two. Let me ask you a follow up question however. How then, would I get my 'february' case to display "Happy Holiday" on either '14' or '2', but "Have a good February!" on any other 'day' inserted?

I didn't realize initially that the (day = 14 || day === 2) was causing any 'day' entry for the 'february' case to result in "Happy Holiday".

If you could show me how this is done within an 'if/else', that is within a 'case', that is within a 'switch' that would be the best! I appreciate your speedy response btw!


#4

Your code Looks fine only the wrong assignment Operator is a mistake:
look at my comments

case 'january':
        if (day = 1) { // day === 1
            console.log("Happy Holiday") 
        }
        else {
            console.log("Have a good January!")
        }
        break;




 case 'february':
        if (day = 14 || day === 2) { // day === 14 
            console.log("Happy Holiday")
        }
        else {
            console.log("Have a good February!")
        }
        break;