4. I Have to scan it

#1

It does not let me go through with this code, however it works for me and this is an easier solution.

var cashRegister = {
total:0,
this.total += itemCost * this.quantity;
},
scan: function(quantity, item) {
this.quantity = quantity;
switch (item) {
}
}
};

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

//Show the total bill

#2

Hi the easier solution (I THINK) will be...

var cashRegister = {
total:0,
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;
}
}
};

// scan each item 4 times
cashRegister.scan("eggs", 4);
...

just add a argument to the scan method the make the operation this.add(0.98 * quantity)

#3

Thanks for the answer, but I don't understand why is it better to repeat "quantity" so many times when the golden rule is not to repeat yourself.
This worked for me finally, without repeating "quantity":

var cashRegister = {
total:0,
this.total += itemCost * this.quantity;
},
scan: function(item, quantity) {
this.quantity = quantity;
switch (item) {
}
}
};

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

//Show the total bill