2016-02-09 119 views
-7

這是一個問題:Switch語句的javaScript

After your prompt, add a switch statement that will test for several different cases (that is, different possible user inputs). Create as many as you like! (Do at least three.) Don't forget to include a default block at the end that will provide a response if the user's choice doesn't match one of your cases.

這是我的代碼:

var user = prompt ("Can you compelete the task?").toLowerCase(); 
switch (the ninja stopped you from moving forward) { 
    case 'fight': 
    console.log("Are you strong enough" && "smart enough");  
    break; 

    case 'run': 
    console.log("Are you fast enough outrun the ninja"); 
    break; 

    case 'pay': 
    console.log("Can you afford to pay the ninja"); 
    break; 

    default: 
    console.log("or will you be defeated by the ninja"); 
};  

這是我得到我去了的代碼,但找不到錯誤對不起錯誤提前我是一個非常新的程序員感謝提前的幫助。

語法錯誤:缺少)開關後表達

+3

你期望什麼「'忍者阻止你前進''做什麼?這不是有效的語法,這就是爲什麼你得到你的語法錯誤。 –

+0

在你的switch語句中,這些詞應該是什麼意思?這只是一句話;它應該是一個表達。 – Pointy

+1

這是[MDN的JavaScript指南](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide)。我建議你開始閱讀,因爲你似乎從根本上失去了。 – Oka

回答

2

你的開關狀態:

the ninja stopped you from moving forward 

無效語法。即使你定義了所有這些變量,你仍然會得到一個錯誤。在你的開關中使用一個真實的條件:

var a = "1"; 
switch(a){ 
    case 1: 
     console.log("Hello"); 
     break; 
    default: 
     console.log("Foobar"); 
} 
+0

哦好吧謝謝我現在看到它新秀的錯誤。 –