Calling cashRegister.add(3.2) set the cashRegister.total to 0 instead of 3.2


#1

Please help me on this one. Not sure what to do.
Keep getting " Try again. Calling cashRegister.add(3.2) set the cashRegister.total to 0 instead of 3.2"
Below is my code. Thanks!!

var cashRegister = {
total: 0,
add: function (itemCost) {
return itemCost += this.total;
},
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;
}
}
};
cashRegister.scan("eggs", 2);
cashRegister.scan("magazine", 3);
//Scan 2 eggs and 3 magazines

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


Cash Register 3.7 "Syntax Error"
#2

Hi this line

return itemCost += this.total

Remove The return And put It like this

this.total += itemCost;

#3

Thanks! it worked.
Just curious why it's not necessary to "return" value? Also, how is " itemCost += this.total" different from "this.total += itemCost" ?? The latter worked, but not the former ( which was what I had in my code).


#4

why it's not necessary to "return" value?

Because You are storing something in a variable so You can return It

how is " itemCost += this.total" different from "this.total += itemCost" ??

Well on this case

itemCost += this.total

You are storing inside itemCost The Sum of itemCost And this.total .... But The lesson aske You to Store It inside this.total so It should be lire that

this.total += itemCost

#5

thank you very much for the explanation


#6

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