Exact Change : Trouble with toFixed(2) and number format


I’m having trouble getting my answer to return in the correct format for currency. I’ve learned about the toFixed() method so you can use .toFixed(2) to display 50 cents as ["0.50"] but it’s a string and it’s supposed to be a number format like [0.50] for your answer.

If I convert to a number with

var change = 0.5;

Results back to displaying 0.5 . I’ve also tried Number.parseFloat() but anytime I convert to a number it drops the zero off the end.

Is there an easy way to simply display as a number with an extra decimal place?

Thank you in advance for any help with this!!


Trailing zeros get stripped from numbers in JavaScript, so the only way to show 0.50 is to display it as a string. You can always do the math on it as a number, and then use .toFixed() later on when you’re ready to display it.


I had this same problem. While I couldn’t find a way to make the trailing zeroes stay in a non-string form, as I thought was required, I was able to pass the test. I did so by switching out .toFixed() with Math.round() in the function I used to make the answer array. Once they were real numbers the tests passed, even though they weren’t formatted like the ‘should return’ sections of the test cases.
I was stuck on this for hours, what with this and javascripts floating point weirdness.

I hope that helps.


been struggling for two days with the strange javascript behavior. I will try your suggestions and feedback. FCC must clarify this for campers.


Hi Do you mean we will be able to pass the test if the format is not right? as long as the change is correct? thank you


Yes. It was a while back that I finished it but I think as long as the format is a number value and not a string it works. The lack of zeroes on the end didn’t matter, as long as everything else was right.