最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - why does this simple switch statement always run the default - Stack Overflow

matteradmin5PV0评论

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

Post a comment

comment list (0)

  1. No comments so far