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???
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.
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);
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,
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.
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);
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:
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?