Cannot find solution. SyntaxError: Unexpected token }


#1

So here is my code. Been having trouble find the issue here.

var cashRegister = {
total: 0,
//insert the add method here
add: function (itemCost) {
this.total += this.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
scan("eggs");
scan("eggs");
scan("magazine");
scan("magazine");
scan("magazine");
//Show the total bill
console.log('Your bill is '+cashRegister.total);


#2

The error is here

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

it should be

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

and also you cannot just call scan() as it is a member of the

 cashRegister so you need to invoke it like this
cashRegister.scan()

#3

ok simple enough. But even after those changes I still get an error. I've been tinkering with it for a while and I cannot seem to identify the problem. I got rid of this.itemCost and instead just put itemCost and even added cashRegister. to the scan method i'm calling out.


#4

Here try this

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

And you can also test it here
Click to go to labs