0

How to use if statement in switch statement

I had to modified a code That i write a number and give me a a day. But i didn't know how to add the condition that if the number is less then 0 or more then 7. https://code.sololearn.com/WrmUDC4qG4Rh/?ref=app

28th Aug 2019, 6:30 AM
Chamseddine Louhmadi
Chamseddine Louhmadi - avatar
19 Antworten
+ 7
Another thing that you could do is set up the switch statement like this: switch(true){ case (day < 0 || day > 7): statements; case (day === 1): statements; ... } Doing this allows you to set up each individual conditional statement as you would in an if block, giving you a little more freedom with your cases (although, at that point you may just want to use an if/else if/else chain)
28th Aug 2019, 7:13 AM
Faisal
Faisal - avatar
+ 2
You should probably, instead of if-statement, put that in your default case instead
28th Aug 2019, 6:38 AM
Trigger
Trigger - avatar
+ 2
Another thing you can do is making the days of the week be in an array and assume the array name is "day" as used by the author. you can easily call on the array on your switch statement. My own little understanding
28th Aug 2019, 8:26 AM
Olorundare Stephen
Olorundare Stephen - avatar
+ 1
Okey thank you i Will try it
28th Aug 2019, 7:08 AM
Chamseddine Louhmadi
Chamseddine Louhmadi - avatar
+ 1
Try this then...... function myFunction() { var day; switch (new Date().getDay()) { case 0: day = "Sunday"; break; case 1: day = "Monday"; break; case 2: day = "Tuesday"; break; case 3: day = "Wednesday"; break; case 4: day = "Thursday"; break; case 5: day = "Friday"; break; case 6: day = "Saturday"; break; default: day = "Unknown Day"; } document.getElementById("txt").innerHTML = "Today is " + day; }
28th Aug 2019, 9:53 AM
Olorundare Stephen
Olorundare Stephen - avatar
+ 1
And this to your HTML line... <!-- Created by Moguiwara--> <!DOCTYPE html> <html> <head> <title>Page Title</title> </head> <body> <p>As soon as you click on the "Click me" button, you see the result</p> <button onclick="myFunction()">Click me</button> <p id="txt"></p> </body> </html>
28th Aug 2019, 9:55 AM
Olorundare Stephen
Olorundare Stephen - avatar
+ 1
I didn't use the array since you said you don't know how to use array...
28th Aug 2019, 9:56 AM
Olorundare Stephen
Olorundare Stephen - avatar
+ 1
Welcome bro. I hope it worked well for you
28th Aug 2019, 10:00 AM
Olorundare Stephen
Olorundare Stephen - avatar
0
Okey i Will try it thank you
28th Aug 2019, 7:15 AM
Chamseddine Louhmadi
Chamseddine Louhmadi - avatar
0
Yes it also a great idea I would like to use it Thank you
28th Aug 2019, 8:30 AM
Chamseddine Louhmadi
Chamseddine Louhmadi - avatar
0
I didn't know how to use array can u give me example please
28th Aug 2019, 8:44 AM
Chamseddine Louhmadi
Chamseddine Louhmadi - avatar
0
Thank you so much
28th Aug 2019, 9:54 AM
Chamseddine Louhmadi
Chamseddine Louhmadi - avatar
0
But where is the array
28th Aug 2019, 9:55 AM
Chamseddine Louhmadi
Chamseddine Louhmadi - avatar
0
Yes right
28th Aug 2019, 9:57 AM
Chamseddine Louhmadi
Chamseddine Louhmadi - avatar
0
Thank qo much
28th Aug 2019, 9:57 AM
Chamseddine Louhmadi
Chamseddine Louhmadi - avatar
0
So*
28th Aug 2019, 9:57 AM
Chamseddine Louhmadi
Chamseddine Louhmadi - avatar
0
Yes
28th Aug 2019, 10:01 AM
Chamseddine Louhmadi
Chamseddine Louhmadi - avatar
0
But i didn't check the html yes
28th Aug 2019, 10:01 AM
Chamseddine Louhmadi
Chamseddine Louhmadi - avatar
0
Yet*
28th Aug 2019, 10:02 AM
Chamseddine Louhmadi
Chamseddine Louhmadi - avatar