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

JavaScript JavaScript Basics (Retired) Working With Numbers Doing Math

Brett Walford
Brett Walford
3,877 Points

How 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

Brett Walford
Brett Walford
3,877 Points

Awesome! Thanks, that worked!

Teacher Russell
Teacher Russell
16,873 Points

Sorry, how do you use that with document.write?

Brett Walford
Brett Walford
3,877 Points

Hey 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>");

Jeffrey Libatique
Jeffrey Libatique
6,701 Points

Same here! hahaha! It worked! Thanx!

Teacher Russell
Teacher Russell
16,873 Points

Can I see how you used that with document.write? I don't understand.

Christopher Phillips
Christopher Phillips
10,061 Points

Russell, 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);
Teacher Russell
Teacher Russell
16,873 Points

Christopher, thanks a lot! Nice and simple.