Building a Cash Register
var cashRegister = {
total: 0,
add: function (itemCost)
{
this.total += itemCost;
},
scan: function (item, quantity)
{
switch(item)
{
case "pencil":
this.add(0.98 * quantity);
break;
case "pen":
this.add(1.23 * quantity);
break;
case "magazine":
this.add(4.99 * quantity);
break;
case "eraser":
this.add(0.45 * quantity);
break;
}
}
s
};
// scan each item 4 times
cashRegister.scan("pencil", 4);
cashRegister.scan("pen", 4);
cashRegister.scan("magazine", 4);
cashRegister.scan("eraser", 4);
//Show the total bill
console.log('Your bill is ' + cashRegister.total);
OUTPUT
Your bill is 30.6In this code snippet, we will be Building a Cash Register using JavaScript.
