var cashRegister = {
total:0,
add: function(itemCost){
this.total += itemCost;
},
scan: function(item,time) {
switch (item) {
case "eggs":
for (var i = 0;i <= time - 1;i++) {this.add(0.98);}; break;
case "milk":
for (var j = 0;j <= time - 1;j++) {this.add(1.23);}; break;
case "magazine":
for (var k = 0;k <= time - 1;k++) {this.add(4.99);}; break;
case "chocolate":
for (var l = 0;l <= time - 1;l++) {this.add(0.45);}; break;
// 上面的代码错在哪里,为什么不能正确实现(下面是可以实现功能的代码)
// 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);
John_Pan
John_Pan
MR帽子先生
舞歌
相关分类