3. Short-Term Memory

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

<Below this line, add a link to the EXACT exercise that you are stuck at.>
https://www.codecademy.com/courses/close-the-super-makert/0/3?curriculum_id=506324b3a7dffd00020bf661

<In what way does your code behave incorrectly? Include ALL error messages.>
SyntaxError: Unexpected identifier

```

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);
    
    case "chocolate"
        this.add(0.45);
    }
    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);

<do not remove the three backticks above>
1 Like

add ,
after

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

8 Likes

im getting this error: SyntaxError: Unexpected token

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);
        
        case "chocolate"
            this.add(0.45);
        }
        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);

1 Like

same problem here, there is a mistake? The problem seems to be in add method, but don’t get it why?

1 Like

rodeman: i finally understan your “add comma” solution. Could you please explain me why? Usually when you finish a function you add the semicolon };

Otherwise at the end of the next method, we don’t finish }, nor }; we just end with }

This is kind off confusing, could you please help me?

1 Like

I believe need a comma after the add function because we are working inside an object and each item inside an object should be separated by a comma. Not entirely sure though, but that would explain it.

1 Like

Yes tragik, I was thinking, the same, and we don’t need it at the end, just in the first and intermediate elements. Thanks for your help! :slight_smile:

1 Like

Does anyone have the correct answer? I have ben trying to work it out for a long time and haven’t succeeded, help!!! why is this wrong?

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

};

cashRegister.scan(“eggs”);
cashRegister.scan(“eggs”);
cashRegister.scan(“magazine”);
cashRegister.scan(“magazine”);
cashRegister.scan(“magazine”);
console.log('Your bill is '+cashRegister.total);

1 Like

i think you should add ; after break in case chocolate

1 Like

This Code works for me.

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;
    
    
    //Add other 2 items here
    
    
    }
    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);

3 Likes

I’m getting this error : unexpected identifier!

1 Like

this code works!!!
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.add(0.98);
cashRegister.add(0.98);
cashRegister.add(4.99);
cashRegister.add(4.99);
cashRegister.add(4.99);
//Show the total bill
console.log('Your bill is '+cashRegister.total);

1 Like

It’s the tiny comma! right after the add function, put a comma and it should run. That’s what the problem was for me anyway. The below code worked for me

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

1 Like

i have the correct answer…

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;

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

1 Like

I have one question I someone would like to have a go.
I got my code to work by listing out each item scanned. but originally i wrote it like this:

cashRegister.scan('eggs' * 2);
cashRegister.scan('magazine' * 2);

Can anyone explain why this wouldn’t work?

1 Like

Hi Mathewvirgen, you are doing a calculation on the item and then sending it to the function. So, the process would attempt to multiple 2 into the word eggs and then send over the output to Scan function.

1 Like

@amelace ahh, yes… i suppose it would.

1 Like

This defeats the whole point of the exercise. It even says it in the question the point is to demonstrate that you can use the name of the shopping item instead of the price.

1 Like

I got confused by this too - hadn’t got my head round the fact that scan was still part of the object (and so the add function should have a comma after it), thanks for the tip!

1 Like

Hi. You forgot the break statements after the two items you added. //Add other 2 items here
case “magazine”:
this.add(4.99);
break;

    case "chocolate":
        this.add(0.45);
        break;
    }
1 Like