5. Bleep Bleep


#1


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


I keep getting an error that says unexpected token and I cant figure out why. Thank you!


Replace this line with your code. 

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

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


#2

You've not finished this line of code. At least based on the comments I see.

cashRegister.scan('chocolate',3);


#3

I'm still getting an error message but thank you for your help!


#4

remove the ;
anyway I have the same problem i get this "SyntaxError: Unexpected token :"
can't find the error .


#5

i found the error in my code

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
voidLastTransaction: function(){
return this.total-=this.lastTransactionAmount;
}

};

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

hope it will help you


#6

Hi cs.girl

Well done on finding the error. Two things about your code that I noticed

  1. lastTransactionAmount:0 is not needed here I believe. The lastTransactionAmount property is created two lines later in the add method. If you go back to your code and remove that line you should still be able to get the right outcome (works for me). So definitely not wrong but perhaps just a bit more code than needed.

  2. this.lastTransactionAmount=+itemCost does not need the + after the =. lastTransactionAmount needs to be set to equal itemCost. The plus is not needed. Remove it and the code will still work. change it to - and you'll get the wrong answer

Not nitpicking, just got stuck on this exercise myself and was looking to see what I did wrong. realizing the things above helped me understand everything a bit better.

Great job!


#7

Now, all your code should be:

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

Still have an issue?


#8

In the start, you need to set the lastTranzactionAmount to itemCost, not totalCost!


#9

Wow..I need a lot more practice. Thanks guys!


#10

You're welcome.

Have fun coding! :slight_smile:


#11

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