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
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)
+ 2
You should probably, instead of if-statement, put that in your default case instead
+ 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
+ 1
Okey thank you i Will try it
+ 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;
}
+ 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>
+ 1
I didn't use the array since you said you don't know how to use array...
+ 1
Welcome bro. I hope it worked well for you
0
Okey i Will try it thank you
0
Yes it also a great idea
I would like to use it
Thank you
0
I didn't know how to use array can u give me example please
0
Thank you so much
0
But where is the array
0
Yes right
0
Thank qo much
0
So*
0
Yes
0
But i didn't check the html yes
0
Yet*