Scan is not defined?


#1

Hello everyone, I am trying to compile this code, and It says that scan is not define?

var cashRegister = {
total:0,
add: function(itemCost){
this.total += itemCost;
},
scan: function(item, quantity) {
switch (item) {
case "eggs": this.add(0.98 * quantity); break;
case "milk": this.add(1.23 * quantity); break;
case "magazine": this.add(4.99 * quantity); break;
case "chocolate": this.add(0.45* quantity); break;
}
return true;
},
};

// scan each item 4 times
scan( 'eggs' , 4);
scan('milk', 4);
scan('magazine' , 4);
scan('chocolate' , 4);

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


#2

Replace this:

// scan each item 4 times
scan( 'eggs' , 4);
scan('milk', 4);
scan('magazine' , 4);
scan('chocolate' , 4);

With this:

// scan each item 4 times
cashRegister.scan( 'eggs' , 4);
cashRegister.scan('milk', 4);
cashRegister.scan('magazine' , 4);
cashRegister.scan('chocolate' , 4);

You need to specify object and only then you can obtain its methods.


#3

Forget it. What I fool. Of course scan is not defined. Sorry for this fool question.
The answer was that scan is a method of cashRegister.

So I have to call cashRegister.scan