2012-04-10 37 views
8

我有一個令人沮喪的時間試圖讓這個工作,Chrome繼續顯示未捕獲的語法錯誤,但作爲一個初學者的JavaScript,我不知道在哪裏看。任何幫助或指針,將不勝感激Javascript - Uncaught SyntaxError:意外的標識符

function details(user) { 
     var fuel = prompt("Would you prefer petrol or diesel?"); 
     var passengers = prompt("How many passengers will there be?"); 
     var aircon = prompt("Do you require air-conditioning?"); 
     var transmission = prompt("Do you want a Manual, Semi-Automatic or Automatic Transmission?"); 
     var hire = prompt("How long would you like to hire a vehicle for? (Day Hire, Weekend Hire or Weekly Hire)"); 

     if (fuel == "petrol" && passengers == "2" && aircon = "yes" && transmission == "semi-automatic") { 
     result = "Lambourghini Aventador"; 
    } else { 
     result = "some form of SUV" 
    } 
     if result = "Lambourghini Aventador") { 

     if (hire == "Day hire") { 
     cost = 2000; 
    } 
     if (hire == "Weekend hire") { 
     cost = 3800; 
    } 
     if (hire == "Weekly hire") { 
     cost = 12000; 
    } 
} 
} 
+2

通常錯誤信息伴隨着一個行號。你甚至可以點擊它,它會告訴你確切的錯誤在哪裏。 – 2012-04-10 11:14:12

+1

快速瀏覽告訴我你錯過了';'在結果=「某種形式的SUV」之後。不知道這是否是問題。 – 2012-04-10 11:14:56

+0

我還會將if語句更改爲if((fuel ==「petrol」)&&(passengers ==「2」)&&(aircon ==「yes」)&&(transmission ==「semi-automatic」) )'。哦,你在'(aircon =「yes」)中缺少'=',那應該是'(aircon ==「yes」)' – 2012-04-10 11:16:31

回答

10

這裏有幾個JavaScript的問題。 你應該使用這個工具:JSLint這是一個非常好的JS質量保證工具,這將驗證你的JS並指出明顯的問題。 :)

第一:

aircon = "yes" 

應該

aircon == "yes" 

其次:

if result = "Lambourghini Aventador") 

應該

if (result == "Lambourghini Aventador") 

第三

result = "some form of SUV" 

應該

result = "some form of SUV"; 

第四

近三成使用==,而是使用JavaScript標準===

閱讀爲什麼here in this very good Stackoverflow post

祝你好運! :)

+6

你提出JSLint並在你的回答中顯示'==' ?現在很諷刺! :p – 2012-04-10 11:36:18

+0

是的,然而他/她可能想用==來出於他們想要的任何原因。我試圖按照他/她的編碼慣例,這就是原因。 – 2012-04-10 11:37:49

+3

PS應該是「Lamborghini」而不是「Lambourghini」。 – jarmod 2013-07-25 16:09:17

相關問題