我在尝试让它工作时遇到了令人沮丧的时间,Chrome 一直显示未捕获的语法错误,但作为 javascript 的初学者,我不知道去哪里找。任何帮助或指示将不胜感激
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;
}
}
}
最佳答案
这里有几个问题。
您应该使用 JSLint这是一个非常好的 JavaScript 质量保证工具。这将验证您的 JavaScript 并指出任何明显的问题。
第一的:
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 !
关于Javascript - 未捕获的语法错误 : Unexpected identifier,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10087628/