3. Short-Term Memory


#1

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


It says: "SyntaxError: Unexpected token ("


#2

Hi can you post all post code like that .

 ```
 Code
 ```

#3

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

#4

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

#5

I did it and it appears "SyntaxError: Unexpected token"


#6

And here add a , after the }

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

#7

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


#8

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

#9

Thank you very much!!!


#10

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

?


#11

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


#12

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


#13

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.


#15