I Have to Scan It More Than Once? [Issue with steps? Alternate Solution w/o console]


#1

The question asks that you:

Modify the scan method such that if we tell it the quantity of each item, it will be able to add the right amount to the total. Since you currently tell scan nothing about quantity, it may be useful to create another parameter.

Scan 4 of each item using your improved scan method. Previously we would have needed to call scan 16 times! Now it is down to 4.

I feel it makes more sense to add onto or create a sub function of which gives the proper answer; or am I overthinking the process?

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

// 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
console.log('Your bill is '+ cashRegister.total);


#2

Check this out!!!!! There is no need to add the "quantity" parameter in "add" method.....
Just make the process like this.add(0.98 * quantity)

var cashRegister = {
total:0,
//Dont forget to add your property
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;
},
//Add the voidLastTransaction Method here

};

cashRegister.scan('eggs',1);
cashRegister.scan('milk',1);
cashRegister.scan('magazine',1);
cashRegister.scan('chocolate',4);

//Void the last transaction and then add 3 instead

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


#3

Sorry for the late response towards this topic, but I had in fact found that same solution from another forum. :slight_frown: But, thank you for your help on the matter as you are in fact correct! Thank you for your time on the matter!


#4

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

Can someone explain to me why is there second argument in add function when the function is defined with only one parameter?If there is second parameter at scan function why " case "eggs": this.add(0.98 * numOfItems);" is as it is and not "case "eggs": this.add(0.98) * numOfItems;" like this?


#5

There isn't a second argument in the add function, only the scan function so that when the quantities are multiplied by the item cost.

To your second question, the scan function takes the two parameters and returns a single one, which is then called into the add function, in which it is then added to the total. Both are separate parts of the equation to achieve the desired result.