0
var speed = prompt("Do you know how to type?");
speed = speed.toLowerCase();
if (speed === "yes" ) {
    var howFast = prompt("what is your wpm?(the answer must be a number between 1 and 200");
    switch(howFast) {
        case (howFast <= 10):
        console.log("you are a snail! practice and type at least 20 wpm, then try this again.");
        break;
        case (howFast <= 30):
        console.log("you are still pretty slow, but you're getting there!");
        break;
        case (howFast <= 50):
        console.log("you are getting there, keep trying");
        break;
        case (howFast <= 90):
        console.log("WoW! Excellent job! Your tenacity has paid off");
        break;
        case (howFast > 90):
        console.log("you are a megaracer! congratulations!");
        break;
        default:
        console.log("DOES NOT COMPUTE... You're either superfast or playing around!");



        }

    } else { alert("learn how to type and comeback.");}

I am trying to code a simple switch statement in javascript to ask users their typing speed. To my dismay, when this code executes the final alert i get back is always the default case. Please tell me what I did wrong!

6 Answers6

3

just change:

switch(howFast) {
..

to

switch(true) {
..

and it should work.
Demo:: jsFiddle

Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
0

Its a bit of a hack but you can do expressions in JS case statements like this:

var wpm = parseInt(howFast); // convert string to number first

switch(true)
{
    case wpm >= 0 && wpm <= 10:
    console.log('snail');
    break;

    case wpm > 10 && wpm <= 30:
    console.log('slowpoke');
    break;

    // etc.
}
Neil Cresswell
  • 1,145
  • 8
  • 20
0

I got this working :

var speed = prompt("Do you know how to type?");
speed = speed.toLowerCase();
if (speed === "yes" ) {
    var howFast = prompt("what is your wpm?(the answer must be a number between 1 and 200");
    switch(true) {
        case (parseInt(howFast) <= 10):
        console.log("you are a snail! practice and type at least 20 wpm, then try this again.");
        break;
        case (parseInt(howFast) <= 30):
        console.log("you are still pretty slow, but you're getting there!");
        break;
        case (parseInt(howFast) <= 50):
        console.log("you are getting there, keep trying");
        break;
        case (parseInt(howFast) <= 90):
        console.log("WoW! Excellent job! Your tenacity has paid off");
        break;
        case (parseInt(howFast) > 90):
        console.log("you are a megaracer! congratulations!");
        break;
        default:
            console.log("DOES NOT COMPUTE... You're either superfast or playing around!");  
    }
    } else { alert("learn how to type and comeback.");}

jsFiddle: http://jsfiddle.net/kR4cy/6/

Hope it helps

I am Cavic
  • 1,115
  • 1
  • 10
  • 22
0

In this case, there isn't an expression that applies to each case, therefore, an if-else block makes more sense to use instead of switch.

-1

From my experience, you cannot have operators in your switch case; you must have a definite value. In this case you should use an IF ELSE block even though the switch would look better.

Edit: I also found this answer from a similar question.

Community
  • 1
  • 1
-2

You need to convert the prompt response to an integer before you compare it, and you will need to change your switch to a set of IFs.

<script>
    var speed = prompt("Do you know how to type?");
    var howFast;
    var logMessage;

    speed = speed.toLowerCase();

    if (speed === "yes" ) {
        howFast = parseInt(prompt("what is your wpm?..."));
        logMessage = "DOES NOT COMPUTE... You're either superfast or playing around!";

        if (howFast <= 10)
            logMessage = "you are a snail! practice and type at least 20 wpm, then try this again.";

        if (howFast <= 30)
            logMessage = "you are still pretty slow, but you're getting there!";

        if (howFast <= 50)
            logMessage = "you are getting there, keep trying";

        if (howFast <= 90)
            logMessage = "WoW! Excellent job! Your tenacity has paid off";

        if (howFast > 90)
            logMessage = "you are a megaracer! congratulations!";

        console.log(logMessage);

    } else {

        alert("learn how to type and comeback.");

    }
</script>
Nick Coad
  • 3,623
  • 4
  • 30
  • 63