5/7 Can't figure this out


#1

So I'd just like to know what's wrong with my code.

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

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


#2

@terarunner93185,

Have a close look at

cashRegister.add('chocolate', 3);


#3

I see that problem now, but I'm getting an error that says "total is not defined"

Edit: found the problem, thanks for the help XD


#4

I don´t understand, what was the problem in the code, I have the same code that you have and I don´t see te problem, can you help me? Thanks!


#5

@cssrunner13664,
Present your code full code, so we can have a look at it....


#6

Here it is, the error message says "SyntaxError: Unexpected token ("

 lastTransactionAmount: 0,
    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() {
        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.add('chocolate', 4);
//Show the total bill
console.log('Your bill is '+cashRegister.total);

#7

@cssrunner13664,
You are not closing your scan Method.....


#8

I´m sorry, but, I see that it is closed, can you show me? Thanks


#9

@cssrunner13664,

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

#10

Thanks! Now it appears "ReferenceError: total is not defined"


#11

@cssrunner13664,

Change your

total -= this.lastTransactionAmount;

into

this.total -= this.lastTransactionAmount;

#12

Thanks! But now it appears an error message that says "Oops, try again. false"


#13

Why are you adding 4 chocolate ????


#14

Now it says "Oops, try again. Did you remember to scan again 3 of the last item?" And when I change cashRegister.scan('chocolate',4); to cashRegister.scan('chocolate',3); The error says again: "Oops, try again. false"


#15

@cssrunner13664,
Give us the full code you are using now.....


#16
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',3);

//Void the last transaction and then add 3 instead
cashRegister.voidLastTransaction();
//Show the total bill
console.log('Your bill is '+cashRegister.total);

#17

@cssrunner13664,
Chnage your

into

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

#18

Oh my god, thank you!!! Thanks for help me all the time! Haha, thank you very much!


#19

you should use a .scan not .add because the scan method add itens on your list