3. Short-Term Memory


#1

I have a "syntax error: Unexpected token } " but I totally can't find this. Please, help!

var cashRegister = {
    total: 0,
    add: function(itemCost) {
        this.total+=itemCost,
    };
    
//insert the add method here    
    
    
    scan: function (item) {
        switch (item) { 
        case "eggs": 
            this.add(0.98); 
            break;
        
        case "milk": 
            this.add(1.23); 
            break;
        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

Remember, if you are adding elements to an object, you separate them using commas ",". This includes variables and functions. You shouldn't have any semicolons on anything being added to an object.

There's also an issue with your add function's body. Remember, only use a comma to separate elements being added to objects. Otherwise, use a semicolon to end a line.


#3

I changed coma to semicolon on the line 4 and now the error is : unexpected identifier :frowning:


#4

So your initial code had 2 errors.
You had

From what you just posted, I think you changed

to

this.total+=itemCost;

which is correct, but you also needed to change the semi-colon on line 5 to a comma like so:

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

Since cashRegister is an object, and we separate properties and their values by a comma, functions in an object (methods) still are separated by commas, and not semi-colons.

Hope this helps!


#5

Thanks! :slightly_smiling:


#6

I want to know why it should have return true; after switch.


#7

@rapeewan I think inside this lesson, the return true; is not being used. but you might find it useful if you want to do some further exploration. for instance you can use the boolean in the if (TRUE){ //stmt} statement
if (cashRegister.scan("eggs")){
//Thanks for buying egg :)
}


#8

Here is my code I don't know whats wrong

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

//insert the add method here

scan: function (item) {
    switch (item) { 
    case "eggs": 
        this.add(0.98); 
        break;

    case "milk": 
        this.add(1.23); 
        break;
    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);

My error is SyntaxError: Unexpected token ';'. Expected '}' to end a object literal.


#9

after of return true:

return true;

}; (here you have to remove the semi-colon)
};