Javascript - 未捕获的SyntaxError:意外的标识符 [英] Javascript - Uncaught SyntaxError: Unexpected identifier
问题描述
我有一个令人沮丧的时间试图让这个工作,Chrome一直显示一个未捕获的语法错误,但作为javascript的初学者,我不知道在哪里看。任何帮助或指示将不胜感激
I'm having a frustrating time trying to get this to work, Chrome keeps displaying an Uncaught Syntax error, but being a beginner to javascript, I have no idea where to look. Any help or pointers would be appreciated
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并指出任何明显的问题。
There are a few problems here. You should use JSLint which is a very good JavaScript quality assurance tool. This will validate your JavaScript and point out any apparent problems.
首先:
aircon = "yes"
应该是
aircon == "yes"
其次:
if result = "Lambourghini Aventador")
应
if (result == "Lambourghini Aventador")
第三次
result = "some form of SUV"
应该是
result = "some form of SUV";
第四次
避免使用 ==
,而是使用JavaScript标准 ===
refrain from using ==
, instead use the JavaScript standard ===
在这篇非常好的Stackoverflow帖子中了解这里的原因!
Read why here in this very good Stackoverflow post!
这篇关于Javascript - 未捕获的SyntaxError:意外的标识符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!