使用方法将扫描的项目添加到总数中

Adding a scanned item to the total using a method

本文关键字:添加 项目 扫描 使用方法      更新时间:2023-09-26

我正在尝试创建一个方法(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/

如果和用于统计,您可能会混淆。如果,只需将替换为,它应该可以工作。