Please help to read my code? 5/7 Bleep Bleep


#1

var cashRegister = {
    total:0,
    lastTransactionAmount:0,
    //Dont forget to add your property
    add: function(itemCost) {
        this.total +=  itemCost;
        this.lastTransactionAmount = itemCost;
    },
    scan: function(item,quantity) {
        switch (item) {
        case "eggs": this.add(0.98 * quantity); break;
        case "milk": this.add(1.23 * quantity); break;
        case "magazine": this.add(4.99 * quantity); break;
        case "chocolate": this.add(0.45 * quantity); break;
        }
        return true;
    },
    //Add the voidLastTransaction Method here
    voidlastTransaction: function() {
        this.total -= this.lastTransactionAmount;
        this.lastTransactionAmount = 0;
    }
};

cashRegister.scan('eggs',1);
cashRegister.scan('milk',1);
cashRegister.scan('magazine',1);
cashRegister.scan('chocolate',4);

//Void the last transaction and then add 3 instead
cashRegister.voidlastTransaction();
cashRegister.scan('chocolate',3);

//Show the total bill
console.log('Your bill is '+cashRegister.total);

It always says "Oops, try again, false".


#2

Hi, thanks for the post. i was able to solve the problem with your help. Having said that, i would remove this line from your code in the method "voidLastTransaction". I'm not sure why you have it.


#3

@creekjohnson50,

You re-initialize the lastTransactionAmount to =zero= AFTER you have used the voidLastTransaction
as you do not want this Method to be used more than once.


#4

@creekjohnson50

Thanks for your kind reply. I've actually tried removing that line "this.lastTransactionAmount = 0;" but the result is still the same.

The reason for this is exactly as @leonhard_wettengmx_n said. To minus the lastTransactionAmount twice is meaningless and it would be safer to reset it after you called this method.


#5

Hi all,

I find the cause.

The name of the method should be "voidLastTransaction" instead of "voidlastTransaction". The L should be in capital.