0

I'm trying to using siwtch case JavaScript like this

 <p id="demo"></p>

  <script>
  var n=new Date();
  var jam=n.getHours();
  switch (jam) {
  case (jam>='17'):
    time = "sleep";
    break;
   case (jam<'17'):
    time = "work";
    break;     
   }
  document.getElementById("demo").innerHTML = "This Time for " + time;
  </script>

but this didn't appear anything

Amani Ben Azzouz
  • 2,477
  • 3
  • 15
  • 26
faza
  • 249
  • 1
  • 8
  • 24
  • 2
    For this case you should use `If() else` state not `switch() case` – xAqweRx Oct 18 '16 at 06:27
  • 1
    http://www.w3schools.com/jsref/jsref_switch.asp read how switch case works. You need to use if else – sinsuren Oct 18 '16 at 06:28
  • Why aren't you using a simple `if else` statement in this case? Otherwise your expression returns a `boolean` and not a number, so the switch case never matches. And also you are comparing integers, so don't use the quotes. – enricog Oct 18 '16 at 06:28
  • define variable `time` – Lahiru Ashan Oct 18 '16 at 06:31
  • Possible duplicate of [Switch on ranges of integers in JavaScript](https://stackoverflow.com/questions/5619832/switch-on-ranges-of-integers-in-javascript) – Herohtar May 13 '19 at 05:08

4 Answers4

2

You are using switch()case in incorrect way. Because switch is used when you are trying to check states, but not ranges. If you want to check ranges, you'd rather use if()else state like this:

<p id="demo"></p>

  <script>
  var n=new Date();
  var jam=n.getHours();

  if ( parseInt(jam) >= 17){
    time = "sleep";
  } else if ( parseInt(jam) <17)
    time = "work";
  } else {
    time = " play :) "
  }

  document.getElementById("demo").innerHTML = "This Time for " + time;
  </script>
xAqweRx
  • 1,236
  • 1
  • 10
  • 23
0

You need to modify the switch case if you are trying to check range using switch, else you have to go for the traditional if else condition. I have modified the code as per your requirement. Please check this one.

 <html>
     <body>
         <p id="demo"></p>

         <script>
                var n=new Date();
                var jam=n.getHours();
                var time = "";
                switch (true) {
                       case (jam>=17):
                              time = "sleep";
                              break;
                       case (jam<17):
                              time = "work";
                              break;     
                 }
                document.getElementById("demo").innerHTML = "This Time for " + time;
         </script>
         </body>
  </html>
Using if else.

    <html>
         <body>
             <p id="demo"></p>

             <script>
                    var n=new Date();
                    var jam=n.getHours();
                    var time = "";
                    
                    if (jam>=17){
                       time = "sleep";
                    }
                                  
                    else if (jam<17){
                       time = "work";
                    }   
                    document.getElementById("demo").innerHTML = "This Time for " + time;
             </script>
             </body>
      </html>
Nitheesh
  • 19,238
  • 3
  • 22
  • 49
0

You may use a case and use a condition in the case statement and true in the select part.

The use a fall trough style to check smalles value first and the bigger values. The check must use numbers, not strings as in the example.

at least you coluld use a default branch and assign somthing default like, which works when all other condition are not met.

var n = new Date(),
    jam = n.getHours(),
    time = '';

switch (true) {
    case (jam < 9):  // this leads to if (true === (jam < 9)) { ...
        time = "coffee";
        break;
    case (jam < 17):
        time = "work";
        break;
    case (jam < 19):
        time = "relaxing";
        break;
    default:
        time = "sleep";
}
console.log("This Time for " + time);
console.log(jam);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

I have fixed your code, here is the js fiddle

https://jsfiddle.net/qkpmkzoh/

And the fixed code is this.

  var n=new Date();
  var jam=n.getHours();
  var time = "";
  console.log(jam);
  switch (true) {
  case jam>=17:
    time = "sleep";
    break;
   case jam<17:
    time = "work";
    break;     
   }
  document.getElementById("demo").innerHTML = "This Time for " + time

However I would recommend you to use if else, as it makes the code more readable

  var n=new Date();
  var jam=n.getHours();
  var time = "";
  console.log(jam);
  if (jam>=17){
   time = "sleep";
  }
    else if(jam<17) {
    time = "work";
  }  
Sumit Vairagar
  • 486
  • 3
  • 12