3: Short-Term Memory my code matches working code but doesn't work


#1


My code matches that of examples I've seen that work but it keeps returning syntaxerror: unexpected token (, but when I look for an extra "(" I can't find any. I've reset the code multiple times and evan completely closed the window and came back to try again but I get this message every time.

var cashRegister = {
    total: 0,
//insert the add method here    
add: function(itemCost){   
    scan: function (item) {
        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

so first we need to write an add method:

add: .... {
    ....
},

then after add (don't forget the ,) you add another method, don't nest the methods

If you need more help, post an updated version of your code


#3

When I moved "scan: function(item)" so it wouldn't be nested under the "add: function(itemCost)" I realized I had "scan: function(item)" listed a second time. After removing that and moving "this.total += itemCost;" so it would be nested under the "add: function" but not the "scan: function" it seemed to work.
My final code was

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


#4

good that it works :slight_smile: do you understand or do you need specific explanation of something?


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.