Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Edmund Southgate
12,056 PointsProblem with .toFixed();
Hello Everyone,
I'm trying out a challenge from a book I bought to create a simple tip calculator as a way of learning JavaScript. I'm trying to get the functionality working so have not spent any time trying to make it look nice yet nor have I cleaned up the code in anyway.
My problem is that I was the totalPerPerson to display in the resultDiv using .toFixed(2) i.e. I don't want it to display if for example someone enters £12.98475928749. However when I alter this line of code bill.textContent = `Bill : ${totalPerPerson}`; to bill.textContent = `Bill : ${totalPerPerson.toFixed(2)}`; (currently line 30 in the pen below) the program stops functioning telling me that it is not a function. However it works with the other values I'm using.
I have no idea why I can't do this. Any solution to this problem would be appreciated.
I've also tried something like totalPerPerson = totalPerPerson.toFixed(2); but this breaks the program too.
1 Answer
Justin Iezzi
18,199 PointsLooks like your totalPerPerson variable was never parsed. By using console.log(typeof totalPerPerson); you can see that it's still a string by the time you use the .toFixed() method.
Edmund Southgate
12,056 PointsEdmund Southgate
12,056 PointsThanks Justin. Now you've said it it seems really obvious. Anyway it works now so I can continue.