2014-01-15 54 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。