5/7 this.lastTransactionAmount


#1

here is my code. it worked, but I am wondering why I need to use "this.total" but not "this.lastTransactionAmount". what does "lastTransactionAmount get to stand alone without the "this.".

In exercise 7, it uses "this.lastTransactionAmount" as the exercise pulls everything together to make one big code.

I messed around with the code in exercise 5, and if added "this" to all the "lastTransactionAmount", the code works. so if it works both ways why do we need to use it?
What is the difference?

var cashRegister = {
    total:0,
    //Don't forget to add your property
    lastTransactionAmount:0,
    add: function(itemCost) {
        this.total +=  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 -= 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

The problem is that in this case you’re not using the lastTransactionAmount that is a property of your object, but you create a global variable with the same name and use this instead:

Code
var cashRegister = {
    total:0,
    //Don't forget to add your property
    lastTransactionAmount:0,
    add: function(itemCost) {
        this.total +=  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 -= 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);
console.log(cashRegister.lastTransactionAmount);
console.log(lastTransactionAmount);

as you can see here cashRegister.lastTransactionAmount is still 0 but lastTransactionAmount has a value.


5/7 bleep^2 - question about quantity
#3

oh ok. I see what you are saying, makes sense. Thank you @haxor789!


#4

I got an answer 8.55. But still can't proceed. Why?


#5

No idea, would you mind post your question and your code and maybe the error message as a seperate question?


#6

The error massage is "Oops, try again. false"

The code is below

var cashRegister = {
total:0,
lastTransactionAmount:0,//Dont forget to add your property
add: function(itemCost) {
this.total += itemCost;
this.lastTransactionAmount=itemCost;
},
voidLastTransactionAmount:function() {this.total-=this.lastTransactionAmount;},
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;
}

};

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

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