1

Ok guys, It seems like this switch statement is forever doomed to NOT work.

The initial idea was to create a variable x which is a prompt, the user will have to select enter any number and that would be the value of x.

Then under the first case of the switch, if x is less than 0.5 then it will simply console.log "less". If x is more than 0.5 it will simply console.log "more". If for some reason the program didn't work as expected the default is to console.log "this is the default"

Then i added a console.log of x in the end just to know what number did the user enter.

Lets try it!

I tried and tried and regardless of what number i enter it always printed "this is the default". Then printed the value of x.

I ended up going Rambo and removing the prompt and declaring x to be 0.6. It ought to print "more" but it still doesn't.

var x = 0.6;

switch (x) {
  case x < 0.5:
    console.log("less");
    break;
  case x > 0.5:
    console.log("more");
    break;

  default:
    console.log("its the dflt");
};

console.log(x);

So I'm wondering whats wrong with this code. Help

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320

3 Answers3

6

switch compares what you switch with against the cases. So, if you have case x < 0.5: which you want to run, that case will run if the expression you switched against was true:

var x = 0.6;

switch (true) {
  case x < 0.5:
    console.log("less");
    break;
  case x > 0.5:
    console.log("more");
    break;

  default:
    console.log("its the dflt");
};

console.log(x);

If you switch against x itself, a case will only run if the case evaluates to the same value as x, which, here, is 0.6, eg:

var x = 0.6;

switch (x) {
  case 0.6:
    console.log('x is exactly 0.6');
    break;
  default:
    console.log("x is something other than 0.6");
};

console.log(x);

But that's not flexible at all, and isn't what you want.

Personally, I'd prefer if/else, it's a lot easier to read (and, as some points out in comments, is a lot faster):

var x = 0.6;
if (x < 0.5) {
    console.log("less");
} else if (x > 0.5) {
    console.log("more");
} else {
    console.log('neither less nor more; equal or NaN');
}
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
  • Or `switch (x>5)`, `case true/false` and `default`, although using a `switch` for OP's use case is rather redundant anyway. – Yannick K Jun 15 '19 at 21:59
  • Please do not use `switch(true)` https://stackoverflow.com/a/12259830/36866 – some Jun 15 '19 at 22:04
0

Switch compares the value of x to the value of the cases. In your code x < 0.5 evaluates to true. Instead of going to that case like if-statements, the switch case compares x and true. Since x is a number, x will never equal true so the default case is always taken.

I would use if-statements instead of a switch in this instance. Switches are better for enumerations (checking if x is a specific value out of a set of values, not a range of values)

0

CertainPerformance has answered you question very well however if you still don't understand how to use switch I would recommend you use "if statements" until you have the time to read more on using switch.

var x = 0.6;

if (x < 0.5) {
  console.log("less");
}
else if (x > 0.5) {
  console.log("more");
}
else {
  console.log("its the dflt");
}

console.log(x);

Hope this is easier for you :)