3. Short Term Memory


#1

I've been working for the last 30minutes to find my "SyntaxError: Unexpected identifier".

I would appreciate any help in pointing out where I've gone wrong.

var cashRegister = {
total: 0,
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);


#2

Properties need to be separated by commas (line 5-6)

Run your code in a different JS interpreter that has line numbers included in its error messages to find stuff like that.


#3

Thank you. I kept looking at it as though the 'add' property were the last property.


#5

A post was split to a new topic: Why the comma after the braces?


#6

add: function(itemCost){
this.total += itemCost;
},--------------------------------Pls why the [comma] after the braces..

it wasn't needed in the exercise before it.
Just wish to understand..


#7

Javarunner, the comma after the curly bracket is part of the "add" property.


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


Properties inside a variable must be separated by commas.

"Scan" is the next property in the variable and contains a function.


#8