Your bill is NaN, but why?


#1

Error: Oops, try again. false

//Everything looks right to me.

var cashRegister = {
    total:0,
    add: function(itemCost, lastTransactionAmount) {
        this.total +=  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;
    },
    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);

cashRegister.voidLastTransaction();
cashRegister.scan('chocolate', 3);

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

#2

so this is your code with one additional console.log i added:

var cashRegister = {
    total:0,
    add: function(itemCost, lastTransactionAmount) {
        this.total +=  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;
    },
    voidLastTransaction: function() {
        console.log(this.lastTransactionAmount)
        this.total -= this.lastTransactionAmount;
        this.lastTransactionAmount = 0;
        }
    
};
cashRegister.scan('eggs',1);
cashRegister.scan('milk',1);
cashRegister.scan('magazine',1);
cashRegister.scan('chocolate',4);

cashRegister.voidLastTransaction();
cashRegister.scan('chocolate', 3);

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

within the voidLastTransaction method, see how it says this.lastTransactionAmount is undefined?

from the instructions:

We need to keep track of how much the last transaction was. Modify the add method to keep track of the amount of the last transaction. This should be tracked in a new property, lastTransactionAmount

this means: modify the function (not adding a method parameter) so this.lastTransactionAmount now knows the cost of the last item bought


#3

What you're saying makes sense, but my code still throws the same error. The only difference in the console is that it now prints "undefined" in addition to "Your bill is NaN"


#4

it does, i was only telling you where the problem was and giving a hint to how you should solve it:


#5

I got it! Thank you! :slight_smile:


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.