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 trialBrett Walford
3,877 PointsHow do you get the seconds alive integer to be displayed with the appropriate commas?
I completed the task and got the answer to how many seconds I've been alive, but it displays it as an integer without commas. How can I get it to display commas?
3 Answers
Abraham Juliot
47,353 PointsUse the .toLocaleString() method on the number. More info at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString
Jeffrey Libatique
6,701 PointsSame here! hahaha! It worked! Thanx!
Teacher Russell
16,873 PointsCan I see how you used that with document.write? I don't understand.
Christopher Phillips
10,061 PointsRussell, I'm pretty new to this myself but I did a bit of research because I was curious as well. It seems you first have to define a new variable that calculates the total seconds in a day. You can't just use the mathematics along with the .toLocaleString() command (I may be incorrect here).
So you could add:
var secondsPerDay = secondsPerMin * minPerHour * HourPerDay;
Your message var:
var message = 'There are ' + secondsPerDay.toLocaleString() + ' seconds in a day';
Then finally:
document.write(message);
Nathan Kilcourse
531 Pointsawesome, thanks you.
Teacher Russell
16,873 PointsChristopher, thanks a lot! Nice and simple.
Brett Walford
3,877 PointsBrett Walford
3,877 PointsAwesome! Thanks, that worked!
Teacher Russell
16,873 PointsTeacher Russell
16,873 PointsSorry, how do you use that with document.write?
Brett Walford
3,877 PointsBrett Walford
3,877 PointsHey Russel, this is what I did:
var secondsPerMin = 60;
var minsPerHour = 60;
var hoursPerDay = 24;
var daysPerWeek = 7;
var weeksPerYear= 52;
var secondsPerDay = secondsPerMin * minsPerHour * hoursPerDay;
document.write("<h3>There are " + secondsPerDay.toLocaleString() + " seconds in a day.</h3>");
var yearsAlive= 34;
var secondsAlive = secondsPerDay * daysPerWeek * weeksPerYear * yearsAlive;
document.write("<br><h2>You have been alive for more than " + secondsAlive.toLocaleString() + " seconds.</h2>");