Ash Davies Ash Davies - 1 month ago 8
Javascript Question

Javascript - Uncaught SyntaxError: Unexpected identifier

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

There are a few javascript problems here. You should use the tool: JSLint which is a very good JS quality assurance tool, this will validate your JS and point out 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!

Good luck! :)