1/7 Error: Unexpected token


#1

This is the code I typed in, and i cant seem to figure out why its not going through. Anyone have an idea?

//Create the object called cashRegister
//and initialize its total property
var cashRegister = {
total: 0;
add: function(itemCost) {
this.total += itemCost;
}

//Using dot notation change the total property
cashRegister.total(2.99);


#2

Object property syntax does not use semi-colons:

var cashRegister = {
    total: 0,
    add: function(itemCost) {
        this.total += itemCost;
    }
};

Note the final closing brace on the object.

Now the assignment. total is not a function, but a variable used here as an accumulator.

cashRegister.total = 2.99;

#4

so is this not the way to do it then ?

var cashRegister = new Object();
cashRegister.total = 0;

cashRegister.setTotal = function(newTotal){
cashRegister.total = newTotal
};

cashRegister.total = 2.99;


#5

For this project, we are building in a literal object model, so, no. It is not wrong, per se, just not what is expected by the SCT (lesson checker).

We will need to write the exact method and property and variable names given in the instructions all the way through this project.

setTotal  =>  add:

The property, total never takes new assignments, only cumulative augmentation, that's why the method is written like so,

    add: function (itemCost) {
        this.total += itemCost;
    }

We are not passing in a new total, only a number to add to the existing total. Since the method is written directly in the object, cashRegister, we refer to the owner context with this.

cashRegister.total = 2.99;

is a window context statement so we must name the object in our identifierName.