3

I'm trying this simple code and seems like the user's input is not going through all the comparisons and jumps to the default one right away. I'm guessing that JS is taking the user's input as a string instead. I did try to parseInt() but didn't work. Here is my code;

var number = prompt('What\'s your favority number?');

switch(number){
    case (number < 10):
        console.log('Your number is to small.');
        break;        
    case (number < 100):
        console.log('At least you\'re in the double digits.');
        break;
    case (number < 1000):
        console.log('Looks like you\'re in three digits.');
        break;
    default:
        console.log('Looks like you\'re in the fouth digits.');
}
redeemefy
  • 4,521
  • 6
  • 36
  • 51

2 Answers2

2

Use true as an expression for switch.

The switch statement evaluates an expression, matching the expression's value to a case clause, and executes statements associated with that case.[Ref]

A switch statement first evaluates its expression. It then looks for the first case clause whose expression evaluates to the same value as the result of the input expression (using strict comparison, ===) and transfers control to that clause, executing the associated statements. (If multiple cases match the provided value, the first case that matches is selected, even if the cases are not equal to each other.) . If no matching case clause is found, the program looks for the optional default clause, and if found, transfers control to that clause, executing the associated statements.

var number = prompt('What\'s your favority number?');
number = Number(number); //Use `Number` to cast it as a number
switch (true) {
  //----^^^^
  case (number < 10):
    console.log('Your number is to small.');
    break;
  case (number < 100):
    console.log('At least you\'re in the double digits.');
    break;
  case (number < 1000):
    console.log('Looks like you\'re in three digits.');
    break;
  default:
    console.log('Looks like you\'re in the fouth digits.');
}
<script src="http://gh-canon.github.io/stack-snippet-console/console.min.js"></script>

Edit: As suggested by @bergi in the comments, an if/else cascade is the best approach for this problem.

var number = prompt('What\'s your favority number?');
number = Number(number); //Use `Number` to cast it as a number
if (number < 10)
  console.log('Your number is to small.');
else if (number < 100)
  console.log('At least you\'re in the double digits.');
else if (number < 1000)
  console.log('Looks like you\'re in three digits.');
else
  console.log('Looks like you\'re in the fouth digits.');
Rayon
  • 36,219
  • 4
  • 49
  • 76
1

You're not understanding how the switch statement works. It is not a shortcut for checking dynamic values. It's a shortcut for checking known values.

Each case is a statement that gets evaluated to a value. If you look at the docs, you'll see that they have case: value, rather than what you are attempting, which is case: (expression). So, it's going to turn all your expressions into values.

So, for example, your first case is:

case (number < 10):

But what that really becomes is:

case false:

And of course, no number will evaluate to false (technically 0 is a falsey value, but the switch uses === comparison rather than == so 0 === false // false). Thus, all your cases are really case false, and so the switch is falling through all of them and landing on the default case.

So, for your situation, the switch statement is inappropriate. You should use if statements.

if(number < 10) {

} else if(number < 100) {

} else if(number < 1000) {

} else {

}

The switch statement is only appropriate when you know the values:

switch(number) {
  case 10:
    break;
  case 100:
    break;
  case 1000:
    break;
  default:
}

(And yes, use parseInt to ensure you have integers.)

Matthew Herbst
  • 29,477
  • 23
  • 85
  • 128