Bleep Bloop problem with variable


#1

https://www.codecademy.com/courses/close-the-super-makert/0/5?curriculum_id=506324b3a7dffd00020bf661#


I'm having an issue with this project. It keeps telling me that it cannot find the variable lastTransactionAmount, but I clearly defined it at the beginning and it should be defined in the add function if there was a problem with the first declaration. I know it is spelled correctly because I copied and pasted the variable name to make sure it wasn't a typing error. Am I missing a comma or semi-colon somewhere? it works without the function call of void last transaction, it just gives me the wrong total amount. Once I call the function apparently the variable stops existing?


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
    voidLastTransactionAmount: function(amount){
        this.total -= amount;
    },
    
};

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

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


#2

HI first this part

    //Add the voidLastTransaction Method here
    voidLastTransactionAmount: function(amount){
        this.total -= amount;
    },

like the comment says you should create a new method named voidLastTransaction then in the instructon they sais that ..
Add a method called voidLastTransaction that subtracts the last amount transacted from total. so it should be like that

    //Add the voidLastTransaction Method here
    voidLastTransaction: function(){
        this.total -= this.lastTransactionAmount;
    },

then here

//Void the last transaction and then add 3 instead
cashRegister.voidLastTransaction(lastTransactionAmount);

since the method (voidLastTransaction) didn't take any paramter so you should call it with an argument but like that

cashRegister.voidLastTransaction();

Then they aske you

Then use the new method to void the last item we scanned.(<-- this part of the instructon you already done it , it's just missing this part -->) Finally, scan only 3 of the same item instead.


#3

Your function should be called voidLastTransaction and should contain 0 parameters so remove the amount parameter. And inside your function you should subtract from the amount like this,

this.total -= this.lastTransactionAmount;

All else is fine. :grin:


#4

Thank you guys, I got it to work using the suggestions you gave. I was just wondering why I don't give a parameter in the function? I realize you can modify two variables created in the larger scope, here the cash register object. However, would using a parameter in the function work the same way or is it doing something else that I'm not understanding?


#5

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