3. Short-Term Memory

Hi, guys, can you help me? Here is my code:


It says: “SyntaxError: Unexpected token (”

Hi can you post all post code like that .

Code

Sure! Here it is:

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

Hi here you should remove the } because it shouldn’t close the cashRegister here

var cashRegister = {
            total: 0,
            add: function(itemCost) {
                this.total+=itemCost
            }
        } <== should remove this one 

And then


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;
    };  <==== remove the ;
};

3 Likes

I did it and it appears “SyntaxError: Unexpected token”

And here add a , after the }

var cashRegister = {
            total: 0,
            add: function(itemCost) {
                this.total+=itemCost
            }, <== this 
        

5 Likes

I did that and it keeps saying “SyntaxError: Unexpected token”, thanks for your help anyway!

1 Like

Here

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

8 Likes

Thank you very much!!!

why im cant make this way:

cashRegister.scan(‘eggs’, 'eggs", ‘magazine’, ‘magazine’, ‘magazine’);

instead

cashRegister.scan(“eggs”);
cashRegister.scan(“eggs”);
cashRegister.scan(“magazine”);
cashRegister.scan(“magazine”);
cashRegister.scan(“magazine”);

?

1 Like

HI (I know its late) But it’s because the method scan only take one argument.

1 Like

Hi, thank you so much for the solution but i’m really confused…why in the objects is it just add and not this.add, and then it’s this.add within the switches…?
Many Thanks

Hi you can find a full explanation in these lesson
Introduction to Objects II - Fun with Functions
Introduction to Objects II - Literally Speaking

In a Object onstructor you can add a method via constructor notation. which is with the key this

//a Constructor Person
 function Person(name, age){
    this.name = name,
    this.age = age,
//here add a method sayHello
   this.sayHello = function() {
      console.log("Hello");
   }
}

#here creat a instance
var marck = new Person("Fred", 0)
//here call the method sayHello
mark.sayHello();

But when you add a method in an Object you do it like that

//object mark
var mark = {
   name: "Fred",
   age: 0,
//here add a method sayHello
   sayHello: function() {
      console.log("Hello")
   }
}

#here call the method sayHello
mark.sayHello();

And the this.add in the switchs I will guese but I think that to access that methid in an other method you need of the key this… In the Lesson
Short-Term Memory
in the Hint they explain it.
Hope that could help you.