More practice with switch


#1


It's only determine a first month in the code like January or April, otherwise it goes to default

var answer = prompt("How many days in month?");

switch(answer) {
  case 'January' || 'March' || 'May' || 'July' || 'August' || 'October' || 'December':
    console.log("It is a 31 days month");
    break;
  case 'February':
    console.log("February is 28/29 days month");
    break;
  case 'April' || 'June' || 'September' || 'November':
    console.log("It is a 30 days month");
    break;
  default:
    console.log("I am not sure, please check your spelling");
 
};


Multiple case options
#2

You can likely find an answer to your issue in this link:


#3

You should change all the || with && that should work :smiley:


#4

Thank you for an advice, but I am interested in using a while loop.
var month = prompt("How many days in month?");
while(month==='January' || month ==='March' || month==='May' || month==='July' || month==='August' || month==='October' || month ==='December') {
console.log("month is 31 days!");
if(month==='April' || month==='June' || month==='September' || month==='November') {
console.log("month is 30 days!");
}
else {
console.log("February is depend from the year!"); month = false;
}

}

That is the answer I got for April:
month is 31 days!
February is depend from the year!
false

Where should I put false?


#5

I am interested in using while loop. so where to put a false condition?


#6

In Python months already assigned to numbers 1-12, I wonder if javascript also assigned it, if so, why assigning month ===1 to 12 did not work? I changed it to a month===January ...etc with || and it's working. but question is the same, does javascript has assigned months? if it is then how to write it?


#7

There is a getMonth method you can read about here:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getDate

You should assign false to month after your console.log statements.

Did you try @s.verheijen's recommendation yet?


#8

var answer = prompt("How many days in month?");

switch(answer) {
case 'January' || 'March' || 'May' || 'July' || 'August' || 'October' || 'December':
console.log("It is a 31 days month");
break;
case 'February':
console.log("February is 28/29 days month");
break;
case 'April' || 'June' || 'September' || 'November':
console.log("It is a 30 days month");
break;
default:
console.log("I am not sure, please check your spelling");

};
That code doesn't work, if you put any other than January for example


#9

In the link I shared, they offer this example:

Use switch-case with a combination of fall-through

switch(input){
case "banana:"
case "guava":
case "apple":
console.log("PASSED!"); break;
}

Maybe this would help.


#10

I just found out through a moderator that the switch can't use a condition. It can only take 1 name like @biff75 has in the case. If you want to use the || && conditions you need to use if/else instead!


#11

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