Javascript - 未捕获的SyntaxError:意外的标识符

Ash*_*ies 7 javascript identifier

我有一个令人沮丧的时间试图让这个工作,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;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Hen*_*son 11

这里有一些问题.您应该使用JSLint,这是一个非常好的JavaScript质量保证工具.这将验证您的JavaScript并指出任何明显的问题.

第一:

aircon = "yes"
Run Code Online (Sandbox Code Playgroud)

应该

aircon == "yes"
Run Code Online (Sandbox Code Playgroud)

其次:

if result = "Lambourghini Aventador")
Run Code Online (Sandbox Code Playgroud)

应该

if (result == "Lambourghini Aventador")
Run Code Online (Sandbox Code Playgroud)

第三

result = "some form of SUV"
Run Code Online (Sandbox Code Playgroud)

应该

result = "some form of SUV";
Run Code Online (Sandbox Code Playgroud)

第四

不要使用==,而是使用JavaScript标准===

在这篇非常好的Stackoverflow帖子中阅读原因!

  • 你提出JSLint并在你的答案中显示`==`?现在这很讽刺!:p (8认同)
  • PS应该是"兰博基尼"而不是"兰博基尼". (5认同)