2014-11-24 109 views
-6
// Check if the user is ready to play! 
var userAnswer = prompt("Do you want to race Bieber on stage?") 
console.log("Suddenly, Bieber stops and says, 'Who wants to race me?'") 
if userAnswer === "yes" { 
console.log("You and Bieber start racing. It's neck and neck! You win by a shoelace!"); 
} else { 
console.log("Oh no! Bieber shakes his head and sings 'I set a pace, so I can race without pacing.'"); 
} 

我試圖做到這一點:!如果userAnswer是肯定的,打印出「你和比伯開始賽車它的脖子和頸部你贏了由鞋帶Javascript代碼給錯誤

否則,打印出來:「哦,不!比伯搖頭,唱歌,我設定了一個速度,所以我可以在沒有步調的情況下比賽。

但它給了我一個語法錯誤。

任何幫助表示讚賞。

+1

'if(userAnswer ==='yes'){}'您需要帶'if'語句的圓括號。 – 2014-11-24 19:25:07

+1

歡迎來到Stackoverflow。但是,在提問時一定要報告所有的細節;當您遇到涉及錯誤的問題時,這些錯誤應該包含在您的帖子中,以便人們可以幫助您確定問題而無需運行代碼。在這種情況下,你遇到了一個語法錯誤,而且這些錯誤通常都帶有一個行號,所以我強烈懷疑,如果沒有我們的幫助,只需按照JS控制檯提供的詳細信息即可找到問題。這也是一個SO政策:首先要遵循你得到的線索。只有在失敗時纔會是一個好主意。 – 2014-11-24 19:29:56

+0

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/if...else – gvk 2014-11-24 19:41:40

回答

2

你的問題是這一行:

if userAnswer === "yes" { 

您需要更改該行:

if (userAnswer === "yes") { 

所以,你的整個代碼應爲:

var userAnswer = prompt("Do you want to race Bieber on stage?") 
console.log("Suddenly, Bieber stops and says, 'Who wants to race me?'") 
if (userAnswer === "yes") { 
    console.log("You and Bieber start racing. It's neck and neck! You win by a shoelace!"); 
} else { 
    console.log("Oh no! Bieber shakes his head and sings 'I set a pace, so I can race without pacing.'"); 
} 

希望這有助於。