# Cash Register with quantity

#1

So i was asked to modify the cashRegister exercise to include quantity and i decided to use a for loop. Although the total is the same at the end than the suggested solution method, it is not accepted as a valid answer. Can someone explain why this would not be a valid method.

var cashRegister = {
total:0,
this.total += itemCost;
},
scan: function(item,quantity)
{
for(var i = 0; i {
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;
}
};
}
};

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

#3

Firstly, your for loop has wrong syntax. This is the correct one.

``````for ( var i = 1; i < someNumber; i++) {
someExecution();
}``````

Secondly, even if you used the for loop, it wouldn't have been a correct solution, since you will multiply the cost of the item by its quantity for too many times (e.g. if there are 3 eggs, on the first loop, it will multiply the cost by 1, then by 2, then by 3, which will result in cost + cost * 2 + cost * 3 = cost * 6, instead of cost * 3!).

Here is my solution for this exercise.

``````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;
}
return true;
}
};

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

Hope this helped ))

#4

yeah it did, thank you.

#5

Good to know. Always glad to help. You should probably mark this post as solved.

#6

Here The other way to solve it

var cashRegister = {
total: 0,
//insert the add method here
this.total += itemCost;
},

``````scan: function (item) {
switch (item) {
case "eggs":
break;

case "milk":
break;
case "magazine":
break;
//Add other 2 items here
case "chocolate":
break;

}
return true;
}``````

};
cashRegister.scan("eggs");
cashRegister.scan("eggs");
cashRegister.scan("magazine");
cashRegister.scan("magazine");
cashRegister.scan("magazine");
//Scan 2 eggs and 3 magazines

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