Bleep Bleep

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;
    },
    voidLastTranscation: function(){
        this.total -=this.lastTransactionAmount
    }
    //Add the voidLastTransaction Method here
    
    
};

cashRegister.scan('eggs',1);
cashRegister.scan('milk',1);
cashRegister.scan('magazine',1);
cashRegister.scan('chocolate',4);
cashRegister.voidLastTranscation();
cashRegister.scan('chocolate',3);
//Void the last transaction and then add 3 instead


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

it returns the ā€œOops, try again. falseā€. I don’t know why???

you have forgotten semicolon

Hi @digitalslayer90169

U didn’t spell right. In line 18. write voidLastTransaction: function() { instead [quote]
voidLastTranscation: function(){
[/quote]
Same in line 30. when u call function.
Actually, ur code will work fine in another place, but in this task u need to write as they ask in instructions.

Check what’s written here:

http://www.w3schools.com/js/tryit.asp?filename=tryjs_local_global

This works:

var cashRegister = {
total:0,
//Dont forget to add your property
add: function(itemCost) {
lastTransactionAmount = itemCost;
console.log(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;
}
last = item;
return true;
},
//Add the voidLastTransaction Method here
voidLastTransaction: function(){
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

console.log(cashRegister.total);
cashRegister.voidLastTransaction();

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

4 Likes

This does not work for me.

I get:

9
ReferenceError: lastTransactionAmount is not defined

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

1 Like

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

someone help me,what is wrong ?

Under your add method, you are using lastTransaction instead of lastTransactionAmount.

Here is the code that works perfect:

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(){
    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);
2 Likes

Here is the code that works perfect:

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

I have a few questions about your code:

when adding a property they normally are followed with ’ : ’ however within the add method the lastTransactionAmount was followed by ’ = ', why is this ? When I did it like this it kept coming up with errors.

Also, I’m a little confused about when to use this. and when not to, I understand from the previous lessons that .this has been used to make a method work for many objects which is why they are helpful within object consructors, but in this case it confuses me for example how to know that you needed to use it for lastTransactionAmount and why it doesn’t work without it,

Many Thanks.

If you look at the above code, here we are declaring a property which is followed by ā€˜:’

And the reason why lastTransactionAmount is followed by ā€˜=’ in the add method is because we are using that property (not declaring as we have already done it).

And since we are accessing it inside a method of an object, we use ā€˜this’ which means whenever we are creating a method for an object and want to access a property of that object inside method, we use ā€˜this’ followed by the property name. It would ensure that we are setting a property of an object even when the parameter names are same as the property itself.

Hope this make sense!! Let me know if you still has any doubts about it.

Thanks

1 Like

For who wants to play and see more in this example, run this more verbose code to better understand what is happening, but it will not work as the solution.

var cashRegister = {

    total: 0,
    lastTransactionAmount: 0,

    add: function (itemCost) {
        this.total += itemCost;
        this.lastTransactionAmount = itemCost;
    },
    scan: function (item, quantity) {
        switch (item) {
            case "eggs": this.add(1 * quantity); 
            console.log("+ eggs: $1 * " + quantity)
            break;
            case "milk": this.add(2 * quantity); 
            console.log("+ milk: $2 * " + quantity)
            break;
            case "magazine": this.add(3 * quantity); 
            console.log("+ magazine: $3 * " + quantity )
            break;
            case "chocolate": this.add(4 * quantity); 
            console.log("+ chocolate: $4 * " + quantity)
            break;
        }
        return true;
    },
    //Add the voidLastTransaction Method here
    voidLastTransaction: function (lastTransactionAmount) {
        console.log("----------")
        console.log("Total - LastTransaction: " + this.total + " - " + this.lastTransactionAmount)
        this.total -= this.lastTransactionAmount;
        console.log("Total after removing: " + this.total)
        console.log("----------")
    }

};

cashRegister.scan('eggs', 1);
cashRegister.scan('milk', 1);
cashRegister.scan('magazine', 1);
cashRegister.scan('chocolate', 1);

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

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

This one is works!

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;
},

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

1 Like

I wanted to ask this question in the ā€œYou Deserve Itā€ section but it has closed down.

i noticed that in the lesson ā€œYou Deserve itā€, the method’s ā€œaddā€ and ā€œvoidLastTransactionā€ are built differently from what i wrote in the previous lesson:

add: function(itemCost){
this.total += (itemCost || 0);
this.lastTransactionAmount = itemCost;

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

can someone explain why ā€œaddā€ has the conditional (itemCost || 0)
and does (this.lastTransactionAmount = 0) just reset ā€œlastTransactionAmountā€ to zero in the instance it may be used again?

hope this is clear

1 Like

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);
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’,3);

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

What am I doing wrong here? Error says ā€œOops, try again. falseā€

Thank you. This was helped me.