5/7 Bleep


#1
var cashRegister = {
    total:0,
    //Dont forget to add your property
    add: function(itemCost, lastTransactionAmount) {
        this.total +=  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(lastTransactionAmount) {
        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('milk',1);
cashRegister.voidLastTransaction('magazine',1);
cashRegister.voidLastTransaction('chocolate',4);
//Show the total bill
console.log('Your bill is '+cashRegister.total);

It keep saying that "Your bill is Nan"
Should lastTransactionAmount:0 be added after the total:0


#2

Here:

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

The lastTransactionAmount is taken as a second argument for scan, but is never used, so there's no point of it being there.
Think about what would define your last transaction.

If I bought eggs, my last transaction would be eggs. But if I then bought some chocolate, my last transaction would be chocolate right?
In your switch statement in scan, when I buy the eggs, it triggers your function add with the argument itemCost (NO lastTransactionAmount!). Then it adds the cost of the eggs (0.98) to your total. itemCost was 0.98 right? Now, when it does the same with chocolate, itemCost becomes 0.45.

You see? itemCost changes for the last item you bought.
Try and figure it out!


#3

Hello @seemmetor, I am facing some different issue with the code. Always getting this error as "Oops, try again.false". Can you please suggest on this? My code is as follows :

var cashRegister = {
total:0,
//Dont forget to add your property
lastTransactionAmount : 0,
add: function(itemCost) {
this.lastTransactionAmount = itemCost;
this.total += 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(){
//console.log(this.total);
this.total = 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('milk',1);
cashRegister.scan('magazine',1);
cashRegister.scan('chocolate',4);
//Show the total bill
console.log('Your bill is '+cashRegister.total);


#4

It only needs the Last Transaction to be deducted. So Chocolates was the last and It'll be removed when we call voidTransactionAmount. Then it says instead of 4 choclates log 3 chocolates.
So the code should be this..

//Void the last transaction and then add 3 instead
cashRegister.voidLastTransaction();
cashRegister.scan('chocolate',3);

This works :slight_smile:


#5

In the voidLastTransaction method, I did the same mistake.

And the code should be :

this.total = this.total-this.lastTransactionAmount;

or

this.total -= this.lastTransactionAmount;

Otherwise, it logs "ReferenceError: lastTransactionAmount is not defined".


#6

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

};

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.voidTransaction();
cashRegister.scan('chocolate',3);

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

this is my code but still I am getting "Oops tryagain false"


#7

It's because you didn't follow the instructions. Edit your code according to the instructions, and it will work :slight_smile:


#8

yup....my bad....overlooked.... :slight_smile:....


#9

Help please! I'm still getting an "oops false" and cant figure out why and know its something little im overlooking. I'm printing out the total after each transaction so i know my void method is actually working and the rest of the math looks correct as well:

0.98
2.21
5.9
10.89
12.690000000000001
/*VOID HAPPENS HERE*/
10.89
/*3 CHOCOLATES ARE ADDED*/
12.24
Your bill is 12.24

I read through a few of these threads and can't figure out what my issue is, any help would be much appreciated. (I even c/p the long variable/method names to ensure consistent spelling) Thanks!!

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(){
this.total -= this.lastTransactionAmount;
},

};

cashRegister.scan('eggs',1);
console.log(cashRegister.total);
cashRegister.scan('milk',1);
console.log(cashRegister.total);
cashRegister.scan('milk',3);
console.log(cashRegister.total);
cashRegister.scan('magazine',1);
console.log(cashRegister.total);
cashRegister.scan('chocolate',4);
console.log(cashRegister.total);
//Void the last transaction and then add 3 instead
cashRegister.voidLastTransaction();
console.log(cashRegister.total);
cashRegister.scan('chocolate',3);
console.log(cashRegister.total);
//Show the total bill
console.log('Your bill is '+cashRegister.total);


#10

disregard!! somehow my original input got screwed up. i removed the extra milk line and it works fine. logic/syntax are sound. thanks!