5. Bleep Bleep


#1

Hello, Can anyone see what is wrong with my code? I am receiving "ReferenceError: voidLastTransaction is not defined
"

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

var cashRegister = {
    total:0,
    //Dont forget to add your property
    add: function(itemCost,lastTransactionAmount) {
        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
voidLastTransaction();
cashRegister.scan('chocolate',3);

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

#2

what is voidLastTransaction() ? isn't a method? how can we access a method?


#3

Also, I am still not clear on why we use this. vs. var.


#4

Ok, I added:

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

print: "Your bill is 10.35"
error: "false."


#5

voidLastTransaction: function() {
           this.total-this.lastTransactionAmount;
           }

you should store this value.it should be

this.total -= this.lastTransactionAmount;


#6

What do you mean by 'you should store this value.'


#7

If I change

voidLastTransaction: function() {
    this.total-this.lastTransactionAmount;
    }

to

voidLastTransaction: this.total-this.lastTransactionAmount;

I get 'SyntaxError: Unexpected token'


#8

your method should be like

voidLastTransaction: function(){
        this.total -= this.lastTransactionAmount;
             }

do you know how it works?why do you need it?
when you call this method it'll subtract the lastTransactionAmount from total.if you dont do this total will remain same.then what is the point to do this?

this.total holds total cost of items and this.lastTransactionAmount only last transaction amount

did you understand every part of your code?

understanding is very impotant than just write it


#9

"when you call this method it'll subtract the lastTransactionAmount from total.if you dont do this total will remain same.then what is the point to do this?" and "this.total holds total cost of items and this.lastTransactionAmount only last transaction amount"
-Yes, I understand that.

-This method is not working (when I add the '=') I receive this message 'SyntaxError: Unexpected identifier:

voidLastTransaction: function(){
        this.total -= this.lastTransactionAmount;
             }

-Honestly, yes, I understand the code, though it is difficult for me and it just takes awhile. I can't remember why we need to add 'this.'


#10

can you please post your full code so i can see it?

after posting select whole code and click on above </> then reply


#11

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

#12

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

here what is the point to do this.lastTransactionAmount - itemCost ?
what should you do here?
what should be this.lastTransactionAmount?

hint: we have to store the last transaction amount


#13

I need another hint, that's not working for me.

I think it is related to this, "Remember that when you add a property or method to an object in literal notation, you should follow it with a , (comma) unless it is the last item of the object." and "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"


#14

what @rcodeman does, it tell you where you go wrong, and why you go wrong. The code still contains the mistake, and he supplies a hint of what you should do instead

Can we see how you implemented his hint? Or do you have questions about the hint?


#15

what is itemCost ? simply the cost of every item.so when you pass last value it'll hold the cost of last item.simply you could do

this.lastTransactionAmount =itemCost


#16

I know + appreceite it a lot, but the same hint 2x twice just isn't helping me. here is what I was thiking but I know it is wrong:

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

#17

could you explain us what this code is doing?

In your words, please explain what the purpose of lastTransActionAmount is.


#18

I am trying to add a property lastTransActionAmount to the add method that will identify the last item purchased so that I can then void out the item purchased with voidLastTransaction.

So, the code should ID the last item purchased and then the voidLastTransaction will subtract that from the total.

This is why I was thiking along the lines of this.lastTransactionAmount = total - itemCost;


#19

pretty close. you want lastTransActionAmount to be the amount of money the last item cost.

Here you are only remembering how much the last item cost. The subtraction is later. So i don't see why you are already subtracting.


#20

I get that but I am still missing something. I am thinking there needs to be a distinction somewhere between itemCost [any item] andlastTransactionAmount [specific item], and I am not seeing how to do it. This is my next attempt, and it is still wrong.

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

Bleep Bleep