JS
上一篇 /
下一篇 2015-08-16 18:49:39
/ 个人分类:JavaScript
Instruction :
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.
var cashRegister = {
total:0,
add: function(itemCost){
this.total += itemCost;
},
scan: function(item,time) {
switch (item) {
case "eggs": this.add(0.98 * time); break;
case "milk": this.add(1.23 * time); break;
case "magazine": this.add(4.99 * time); break;
case "chocolate": this.add(0.45 * time); break;
}
}
};
// scan each item 4 times
var myArr = ["eggs","milk","magazine","chocolate"];
for ( var m = 0;m <= myArr.length - 1 ; m++ ) {
cashRegister.scan(myArr[m],4);
};
//Show the total bill
console.log('Your bill is '+cashRegister.total);
收藏
举报
TAG: