Javascript - Uncaught SyntaxError: Unexpected identifier

。_饼干妹妹 提交于 2019-12-05 22:37:44

问题


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;
        }
    }
}

回答1:


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.

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 ===

Read why here in this very good Stackoverflow post!



来源:https://stackoverflow.com/questions/10087628/javascript-uncaught-syntaxerror-unexpected-identifier

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!