Javascript - 未捕获的语法错误:意外的标识符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10087628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 23:48:20  来源:igfitidea点击:

Javascript - Uncaught SyntaxError: Unexpected identifier

javascriptidentifier

提问by Ash Davies

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

我在尝试让它工作时遇到了令人沮丧的时间,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;
        }
    }
}

回答by Henrik Andersson

There are a few problems here. You should use JSLintwhich is a very good JavaScript quality assurance tool. This will validate your JavaScript and point out any apparent problems.

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

First:

第一的:

aircon = "yes"

should be

应该

aircon == "yes"

secondly:

其次:

if result = "Lambourghini Aventador")

should be

应该

if (result == "Lambourghini Aventador")

thirdly

第三

result = "some form of SUV"

should be

应该

result = "some form of SUV";

fourthly

第四

refrain from using ==, instead use the JavaScript standard ===

避免使用==,而是使用 JavaScript 标准===

Read why here in this very good Stackoverflow post!

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