5/7 Still struggling


#1

Hi there. I'm still struggling with 5/7 despite someone's helpful advice. Please see my code below. the error message I'm getting is the following:

Oops, try again. It looks like you have an error in your code! Check the error
message for more details: ReferenceError: lastTransactionAmount is not
defined

Desperate to get this module finished as I'm so close to the end now. Appreciate your feedback.

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

};

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);


#2

Hi, at this line you should use this

this.lastTransActionAmount += itemCost;

#3

Hi pointphoton, thansk for your guidance. I have made the amend but now it is returning the following response - "Oops, try again. false". In the console it says "Your bill is NaN". Any thoughts?


#4

There is a typo error. > lastTrans**A**ctionAmount !==lastTrans**a**ctionAmount


#5

I am getting the same error (it says it is false), but I believe my code is right. Can you help me? Thank you. Here is my code:

var cashRegister = {
total:0,
lastTransactionAmount: 0,
add: function(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;
},
voidLastTransaction: function(){
this.total -= this.lastTransactionAmount;
}
//Add the voidLastTransaction Method here

};

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

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

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


#7

add() function should be like this.

   add: function(itemCost) {
        this.total +=  itemCost;
        this.lastTransactionAmount =itemCost;
        
    },

#8

is the answer not 8.55?

Compilers is telling me this answer is false, but when I calculate by hand thats what I get.

Here is what I have:
start -> total = 9
remove last transaction (remove 4 chocolates) -> total = 7.20
add 3 chocolates -> total = 8.55

Code is working fine and producing expected result, but compiler says false...


#9

The line you are talking about should be this.lastTransactionAmount = itemCost not += coz otherwise you would just be setting the lastTransactionAmount to 0.

var cashRegister = {
    total: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;
    }

};

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);