2014-02-20 23 views
-1
var age = Number(prompt("How old are you?")); 

if(age >= 13) { 
    console.log("You are old enough!"); 
    alert("You Are old enough!"); 
    console.log("You are at a Justin Bieber concert, and you hear this lyric 'Lace my shoes off, start racing.'"); 
    console.log("Suddenly, Bieber stops and says, 'Who wants to race me?'"); 

    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.'"); 
    } 
} else { 
    console.log("Sorry, You Are Not old Enough :("); 
    alert("Sorry, You Are Not old Enough :("); 
} 

我越來越:JavaScript的菜鳥級語法錯誤

SyntaxError: Unexpected token 'else'

如果有人可以幫助我,將是巨大的:)

+5

變化'如果(userAnswer) 「是」;''來,如果(userAnswer == 「是」){' – Bart

+0

@SamuelLiew您剛纔編輯自己的錯誤離開這個問題?沒有人知道上下文了。 – A1rPun

回答

2

這是系統的提示,需要比較userAnswer您的價值:

if (userAnswer) "yes"; 

無效。改變

if (userAnswer == "yes") 

然而,這隻會工作,如果用戶鍵入「是」準確,而不是「OK」,「是啊!」或類似的。

爲了避免這種情況,您可以使用確認對話框,它將提供兩個選項「確定」或「取消」。

if(confirm("Do you want to race Bieber on stage?")) { 
+1

其實你呢。提示需要用戶輸入字符串,因此如果用戶輸入「否」,提示將返回「否」,其結果爲「真」。 – Bart

1

中,如果循環

age = prompt(" How old Are You ? "); 
if(age >= 13) { 
    console.log("You Are old Enough ! "); 
    alert("You Are old Enough ! "); 
    console.log("You are at a Justin Bieber concert, and you hear this lyric 'Lace my shoes off, start racing.'"); 
    console.log("Suddenly, Bieber stops and says, 'Who wants to race me?'"); 
    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.'"); 
    } 
} else { 
    console.log("Sorry, You Are Not old Enough :("); 
    alert("Sorry, You Are Not old Enough :("); 
} 
+0

如果不是循環而是控制(條件)結構;) – Christoph

2

你在你的第二個if語句有錯誤刪除;

if (userAnswer == "yes"); 
-1

因爲顯然沒有人知道,及時返回一個字符串,在這裏我們去:

age = prompt(" How old Are You ? "); 
if(age >= 13) { 
    console.log("You Are old Enough ! "); 
    alert("You Are old Enough ! "); 
    console.log("You are at a Justin Bieber concert, and you hear this lyric 'Lace my shoes off, start racing.'"); 
    console.log("Suddenly, Bieber stops and says, 'Who wants to race me?'"); 
    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.'"); 
    } 
} else { 
    console.log("Sorry, You Are Not old Enough :("); 
    alert("Sorry, You Are Not old Enough :("); 
} 
3

您需要更改

if (userAnswer) "yes"; 
{ 
} 

if (userAnswer == "yes") 
{ 
} 

爲什麼?

聲明if (userAnswer) "yes";表示檢查userAnswer是否有一個值(if(userAnswer)),然後是一個意外的令牌"yes";

正確的答案是如何工作的?

if (userAnswer == "yes")表示通過使用==運算符來檢查userAnswer是否等於「yes」,該運算符是許多編程語言中的常見比較運算符。

提示:您可能希望使用if (userAnswer.toUpperCase() == "YES"),因爲它意味着yes可以在任何情況下進入,例如yEsYEs ...

0

prompt更改爲confirm並檢查返回的布爾值會更穩健。

使用prompt將返回一個字符串,您明確必須檢查某個值(例如,通過正則表達式或一組可能的有效答案)。如果用戶鍵入For sureHell, yeah!或甚至簡單YES您的支票userAnswer == "yes"將失敗。

所以最好使用這個:

var userAnswer = confirm("Do you want to race Bieber on stage? "); 

if (userAnswer){ 
    /*...*/