使用方法将扫描的项目添加到总数中
Adding a scanned item to the total using a method
我正在尝试创建一个方法(add:),在扫描时将产品价格添加到总价格中。这是我的东西,但它不起作用。
var cashRegister = {
total: 0,
//insert the add method here
add: function (itemCost){
if(var i = this.itemCost; i >=0; i++){
return cashRegister.total + this.itemCost;
}
},
scan: function (item) {
switch (item) {
case "eggs":
this.add(0.98);
break;
case "milk":
this.add(1.23);
break;
//Add other 2 items here
case "magazine":
this.add(4.99);
break;
case "chocolate":
this.add(0.45);
break;
}
return true;
}
};
var cashRegister = {
total: 0,
//insert the add method here
add: function (itemCost) {
if (itemCost >= 0) {
this.total += itemCost;
}
},
scan: function (item) {
switch (item) {
case "eggs":
this.add(0.98);
break;
case "milk":
this.add(1.23);
break;
//Add other 2 items here
case "magazine":
this.add(4.99);
break;
case "chocolate":
this.add(0.45);
break;
}
return true;
}
};
cashRegister.scan('eggs');
cashRegister.scan('magazine');
alert(cashRegister.total);
给你。你在add()中的if语句太离谱了。下面是它的小提琴:http://jsfiddle.net/c453Lx4r/1/
如果和用于统计,您可能会混淆。如果,只需将替换为的,它应该可以工作。
相关文章:
- 在Javascript中,当使用array.prush方法向列表中添加项目时,如何对其进行排序
- 在不向上滚动的情况下向列表框中添加项目
- 为图像滑块jquery添加项目符号功能
- 在Javascript中使用JQueryMobile向列表添加项目
- ng只重复添加项目一次
- jCarousel-动态添加项目
- 如何在控制器对添加项目执行操作后更新视图
- SharePoint 2013 在添加项目上运行 Javascript
- 从选择框中删除项目或向选择框添加项目
- 如何在 ExtJS5 中网格编辑器组合的当前记录中添加项目
- 反应多个组件添加项目
- 如何在KeystoneJS中以编程方式从页面添加项目
- 正在添加项目id's到js-if语句
- Rails-如何在添加项目时强制刷新页面
- AngularJS+simpleCart问题显示购物车内容,直到添加项目
- 按$index添加项目的角度过滤器
- OwlCarousel 2.0.0-beta.2.4-可以预先添加项目,但不能使用'add.wowl.carou
- 允许使用向asp.net mvc4模型添加项目
- 在Javascript中按下按钮时向列表中添加项目
- contentflow通过函数添加项目