3. Short-Term Memory


#1



https://www.codecademy.com/courses/close-the-super-makert/0/3?curriculum_id=506324b3a7dffd00020bf661


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


#2

add ,
after

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


#3

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

#4

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


#5

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?


#6

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.


#7

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:


#8

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


#9

i think you should add ; after break in case chocolate


#11

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


#12

I'm getting this error : unexpected identifier!


#13

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


#14

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


#15

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


#18

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?


#19

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.


#20

@amelace ahh, yes... i suppose it would.


#21

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.


#22

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!


#23

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