admin管理员组

文章数量:1432595

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);

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

Share Improve this question edited Jun 15, 2019 at 22:00 CertainPerformance 372k55 gold badges352 silver badges357 bronze badges asked Jun 15, 2019 at 21:52 Cool_berserkerCool_berserker 471 silver badge7 bronze badges 1
  • Thanks certainperformance, i was sure someone was bewitching my code lol – Cool_berserker Commented Jun 16, 2019 at 7:24
Add a ment  | 

3 Answers 3

Reset to default 6

switch pares 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 ments, 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');
}

Switch pares 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 pares 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)

CertainPerformance has answered you question very well however if you still don't understand how to use switch I would remend 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 :)

本文标签: javascriptwhy does this simple switch statement always run the defaultStack Overflow