I have to scan it more than once


#1

whats wrong , when i use this it print out" Your bill is 0eggsmilkmagazinechocolate"
var cashRegister = {
total:0,
add: function(itemCost , quantity){
this.total += itemCost;
this.quantity = quantity;

},
scan: function(item) {
    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;
    }
}

};
cashRegister.add("eggs", 4);
cashRegister.add("milk", 4);
cashRegister.add("magazine", 4);
cashRegister.add("chocolate", 4);

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


#2

Should this be a 'scan'?


#3

i change that and then it said
"ReferenceError: quantity is not defined"

var cashRegister = {
total:0,
add: function(itemCost , quantity){
this.total += itemCost*quantity;
this.quantity = quantity;

},
scan: function(itemCost) {
    switch (itemCost) {
    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;
    }
}

};
cashRegister.scan("eggs", 4);
cashRegister.scan("milk", 4);
cashRegister.scan("magazine", 4);
cashRegister.scan("chocolate", 4);

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


#4

"ReferenceError: quantity is not defined"

The parameter is missing. The call contains two arguments, but the function is taking but one. Add the quantity parameter and things will be set right.


#5

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