简单的开关声明

时间:2014-01-15 04:00:30

标签: javascript switch-statement

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.");}

我正在尝试在javascript中编写一个简单的switch语句来询问用户的打字速度。令我沮丧的是,当这段代码执行最终警报时,我回来的情况总是默认情况。请告诉我我做错了什么!

6 个答案:

答案 0 :(得分:3)

只是改变:

switch(howFast) {
..

switch(true) {
..

它应该工作。
演示:: jsFiddle

答案 1 :(得分:0)

它有点像黑客但你可以在JS case语句中做这样的表达式:

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.
}

答案 2 :(得分: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(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/

希望有所帮助

答案 3 :(得分:0)

在这种情况下,没有适用于每种情况的表达式,因此,使用if-else块更有意义而不是切换。

答案 4 :(得分:-1)

根据我的经验,你的开关盒中不能有操作员;你必须有一个明确的价值。在这种情况下,即使切换看起来更好,也应使用IF ELSE块。

编辑:我还从类似的问题中找到了this的答案。

答案 5 :(得分:-2)

在比较之前,您需要将提示响应转换为整数,然后您需要将切换更改为一组IF。

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