2015-02-08 63 views
-4

意外的標識符在運行下面的代碼我得到一個語法錯誤:語法錯誤:在JavaScript

var userAnswer = prompt("Do you want to race Bieber on stage?") 
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.'") } 

有比上面的線條更加的代碼,但我知道錯誤從那裏起源。

+2

'如果userAnswer =( 「是」)'< - - 這是什麼?你在哪裏找到'if'這樣一個奇怪的語法? – zerkms 2015-02-08 21:51:09

+0

更改爲:'if(userAnswer ==「yes」)' – 2015-02-08 21:52:23

回答

4

您的if語法錯誤。它應該是這樣的:

if (userAnswer === "yes") 
1

你的if-then語法看起來不對,它應該是if (expression) {} else {}。您還使用賦值運算符在if表達式(userAnswer = ("yes")),而不是測試平等(userAnswer == "yes"

嘗試以下操作:

if (userAnswer == "yes") { 
    console.log("You and Bieber...") 
} 
else { 
    console.log("Oh no! ...") 
}