Javascript - Uncaught SyntaxError: Unexpected identifier

Ash Davies picture Ash Davies · Apr 10, 2012 · Viewed 57.1k times · Source

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

Answer

Henrik Andersson picture Henrik Andersson · Apr 10, 2012

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!