Short Term Memory


#1


Please help me! When I submit the code it says Syntax Error Unexpected Identifier but I can't find the problem...


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 "milk": 
            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);


#4

My code looks the same as yours, aside from the second milk thing.


#5

I reckon it's the duplicate Milk in yours webwhiz.
I think you copied and pasted and forgot to change the second milk to chocolate.

FYI, your code helped me with mine. I was getting the same error and couldn't find where either. I saw yours and immediately realised I forgot to put ' this. ' in front of ' total += itemCost; ' so thank you. :slight_smile:


#6

Thanks to everyone for their suggestions, I eventually found my error which was a missing comma after the bracket on line six! I'm putting my code so it can maybe help others in the future?

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;

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

#7