2014-01-15 112 views
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.");} 

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

+0

试试'switch(parseInt(howFast))' –

回答

3

只是改变:

switch(howFast) { 
.. 

switch(true) { 
.. 

,它应该工作。
演示:: jsFiddle

-1

从我的经验来看,你的交换机箱中不能有操作员;你必须有一个确定的价值。在这种情况下,即使交换机看起来更好,也应该使用IF ELSE块。

编辑:我也发现this类似问题的答案。

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

希望它有帮助

0

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