2. Manually Add It Up?


#1

There's something is wrong with my code? i have no idea can u explain and help me?
the output -> Your bill is 7.56
but it says "Oops, try again. Have you called the method four items to add each itemCost?"

var cashRegister = {
total:0,
add: function(itemCost){
this.total += itemCost;
}
};

//call the add method for our items
cashRegister.add(0.89);
cashRegister.add(1.23);
cashRegister.add(4.99);
cashRegister.add(0.45);

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


#2

total is a variable so create using an equal sign = and not a colon :


#3

oh my god thank you so much i didnt notice that!! thanks man


#4

I get the message: "Oops, try again. Have you called the method four items to add each itemCost?" for the code:

var cashRegister = {
total:0,
add: function(itemCost){
this.total += itemCost;
}
};

//call the add method for our items
cashRegister.add(0.89);
cashRegister.add(1.23);
cashRegister.add(4.99);
cashRegister.add(0.45);

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


#5

But I can see the result: "Your bill is 7.5600000000000005"


#6

i tried this. it doesn't work in my case.


#7

The same goes for you here assign the value 0 total using an equal sign, = not a colon, :


#8

I tried with equals sign, it doesn't work, perhaps because "total" it's the property and "0" is the value, "total" doesn't seem to be a variable in this case


#9

You are right total is not a 'normal' variale but still a variable in abstract sense, your code is correct in using a colon to assign 0 to the variable total but be sur =e it is written in this format,


#10

:slight_smile:
When I use total=0, the answer I receive is: "Oops, try again. There was a problem with your syntax."
In fact, that part of the code was already provided, the request of the exercice was to "//call the add method for our items"


#11

Yes that is why I have just said,

Have you taken a loo at the example code I have given above. :slight_smile:


#12

I even did something worse hahaha I coppied and pasted it :smiley: either I'm crazy or dumb .....


#13

Haha I don't think you are either of the two, we all make mistakes sometimes and that's why programming is intriguing because we learn to solve the hard problems and the easy ones as well. :sweat_smile:

I digress, having corrected the errors is your code working now?


#14

And thank you for your patience!


#15

You're super welcome :grin:


#16

It didn't work with equals sign or with color, neither in Firefox or Chrome.
I tried another code, though I don't think it's what I was supposed to write... Anyway, this time it worked. So, this is it:

var cashRegister = {
total:0,
add: function(itemCost){
this.total += itemCost;
}
};

//call the add method for our items
var eggs = 0.98;
var milk = 1.23;
var magazine = 4.99;
var chocolate = .45;

cashRegister.add(eggs + milk + magazine + chocolate);

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


#17

This block of code is incorrect,

just revert back to how you originally wrote it,

//call the add method for our items
cashRegister.add(0.98);
cashRegister.add(1.23);
cashRegister.add(4.99);
cashRegister.add(0.45);
//Show the total bill
console.log('Your bill is '+cashRegister.total);

and it should work. :slight_smile:


#18

This is magic :sunglasses: It works now


#19

JavaScript is bad at handling floating point math at times. If everything should work skip the exercise.
Is floating point math broken? --stackoverflow


#20

It doesn't work because instead of 0.98 it is listed as 0.89, it's just a typo...:sweat_smile: