5. Logical operators


#1



Could anyone kindly let me know why this code does not work properly? Try 1 and 1 when prompted, and it says switch's default, even though it should match one of the cases.

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


var month = prompt("month (in number)?");
var day = prompt("day?");

switch (month) {
    case 1:
        if (day === 1) {
            console.log("Happy Holiday") 
        }
        else {
            console.log("Have a good January!")
        }
        break;
    case 2:
        if (day === 14 || day === 2) {
            console.log("Happy Holiday")
        }
        else {
            console.log("Have a good February!")
        }
        break;
    case 5:
        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

OK, I googled and inserted line 4 and 5, using 'parseInt'.

var month = prompt("month (in number)?");
var day = prompt("day?");

var month = parseInt(month);
var day = parseInt(day);

switch (month) {
    case 1:
        if (day === 1) {
            console.log("Happy Holiday") 
        }
        else {
            console.log("Have a good January!")
        }
        break;
    case 2:
        if (day === 14 || day === 2) {
            console.log("Happy Holiday")
        }
        
        else {
            console.log("Have a good February!")
        }
        break;
    case 5:
        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;
}

#3

seems fine now? Except this condition:

if (day === 30 && day === 31)

is a bit impossible. A variable can't have two integer values at the same time


#4

Thank you, that's true! Just didn't come up with a nice idea to get through the test.


#5

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