3.Short-Term Memory


#1



i do not know where i went wrong.


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

What error message are you getting?
A problem I see is that

Is not indented, as well as it being mispelled.


#3

I know what your problem is. The message you're getting is

SyntaxError: missing : after property id

Well, here is your problem

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

You should not have var there, it should be

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

The rest of your code is fine and will still run, but you should edit this part

case "choolate" :
this.add(4.99);
break;

You need to indent it to match the other cases, you forgot the second c in chocolate, and you don't need a space. Use this instead

case "chocolate":
this.add(4.99);
break;

Happy coding!


#4

You're right, I completely missed that.


#5

It's alright, another thing I just noticed

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

After the var is removed, the code will still run without the return, so you can use this

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

You can keep the return, but it still runs without it.


#6

It still doesnt work

current code:

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 "chocolate" :
            this.add(4.99);
            break;
            
        case "magazine":
        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

Your code is almost correct, aside from not being properly indented, but that isn't what's breaking your code. The prices, you have them switched.

    case "chocolate" :
        this.add(4.99);
        break;
        
    case "magazine":
    this.add(0.45);
    break;

Use this

    case 'magazine':
        this.add(4.99);
        break;

    case 'chocolate':
        this.add(0.45);
        break;

I know, it sounds crazy, but that is your problem.

Happy Coding!


#8

Oops, try again.
Have you scanned eggs twice and magazine three times?
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("magazines");
cashRegister.scan("magazines");
cashRegister.scan("magazines");

//Show the total bill
console.log('Your bill is '+cashRegister.total);


#9

I can't find out wrong who help me?


#10

Your problem is very simple, you scanned magazines 3 times, not magazine.

cashRegister.scan("magazines");

You are supposed to use

cashRegister.scan("magazine");

Your code tells the cash register what to do when it scans a "magazine" the cash register does not know what a "magazines" is.


#11

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