2012-04-10 95 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

相关问题