3/7 Syntax Error: Unexpected Token }


#1
var cashRegister = {
    total: 0,
//insert the add method here    
    add: function(itemCost) {
        this.total += itemCost,
    };
    
    scan: function (item) {
        switch (item) {
        case "eggs": 
            this.add(0.98); 
            break;
        
        case "milk": 
            this.add(1.23); 
            break;
        
        //Add other 2 items here
        case "magazine":
            this.add(4.99);
            break;
        
        case "chocolate":
            this.add(0.45);
            break;
        }
        return true;
    }
    
};

//Scan 2 eggs and 3 magazines


cashRegister.scan("eggs");
cashRegister.scan("eggs");
cashRegister.scan("magazine");
cashRegister.scan("magazine");
cashRegister.scan("magazine");

//Show the total bill
console.log('Your bill is '+cashRegister.total);

I've also tried `this.total += itemCost;' (with a semi-colon), which is what some of the other threads said to do. But all I get is a "Syntax Error: Unexpected Token".

Thank you in advance.


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

The last semi-colon is causing you trouble. Do you know why?


#3

Thank you.

Well I've never thought of it until you asked, but my guess would be because it's not the end of the whole function, it's just the end of a nested function.

Am I at all in the ballpark?

I've changed it, but it's still showing an unexpected }


#4

Did you remove it? You had to change it to ,.

But why?

Well, you are defining an object cashRegister by literal pattern (using {} and then listing properties). So, it should be like this:

var cashRegister = {
    prop1: value,
    prop2: value,
    prop3: value,
    etc.
};

Note that we use comma (,) to delimit one property from another. But then see what you are doing:

var cashRegister = {
    total: 0, // comma ok

    add: function(itemCost) {
        this.total += itemCost,
    };         // semi-colon ????
    
    scan: function (item) {
        switch (item) {
        // .. irrelevant code follows

Can you now see your error?

Hope it helps! :smiley: