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

Sean Flanagan
Sean Flanagan
33,235 Points

My answer

Hi.

Here's my solution:

var secondsPerMinute = 60;
var minsPerHour = 60;
var hoursPerDay = 24;
var daysPerWeek = 7;
var weeksPerYear = 52;
var secondsPerDay = secondsPerMinute * minsPerHour * hoursPerDay;
document.write("There are " + secondsPerDay + " seconds in a day. <br>");
var yearsAlive = 43;
document.write("I've been alive for more than " + (yearsAlive * secondsPerDay) + " seconds!");

How does it look? Any room for improvement?

Sean

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! I originally left an answer that was partially correct until I looked at the math. And while it's true that you are older than that number of seconds, you're multiplying the number of years you've been alive by the number of seconds in a day. But you should be multiplying the number of years you've been alive by the number of seconds in a year.

With that in mind, I've rewritten your code a bit to include some newer syntax and adjust your math.

const secondsPerMinute = 60;
const minsPerHour = 60;
const hoursPerDay = 24;
const daysPerWeek = 7;
const weeksPerYear = 52;
const secondsPerDay = secondsPerMinute * minsPerHour * hoursPerDay;
const secondsPerYear = secondsPerDay * daysPerWeek * weeksPerYear;
let yearsAlive = 43;

document.write(`There are ${secondsPerDay} seconds in a day. <br>`);
document.write(`I've been alive for more than ${ yearsAlive * secondsPerYear} seconds!`);

I still left your secondsPerDay intact but then used it to further calculate out the secondsPerYear. This is what I multiplied by yearsAlive.

Hope this helps! :sparkles:

Sean Flanagan
Sean Flanagan
33,235 Points

Hi Jennifer.

So instead of multiplying yearsAlive by secondsPerDay, I should have multiplied yearsAlive by secondsPerYear?

Thanks

Sean

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

In my opinion, yes. Well let's take this example. If I were to multiply the number of seconds in a day by 6, what am I actually saying? I'm saying the number of seconds in six days... not 6 years. By multiplying the number of seconds per day by 43, you are saying that you're older than 43 days old :smiley: And while this is correct, it's probably not what you were after.

It feels like you were after how many seconds you've had in your life, which I'm guessing is at around 43 years. So you would want the calculation for the number of years times the seconds per year.

Make sense? :sparkles:

One more thing: Because these are fairly large numbers, you can make the output a bit nicer by including the toLocaleString() function in your output to format the numbers.

document.write(`There are ${secondsPerDay.toLocaleString('en')} seconds in a day. <br>`);
document.write(`I've been alive for more than ${(yearsAlive * secondsPerYear).toLocaleString('en')} seconds!`);
Sean Flanagan
Sean Flanagan
33,235 Points

Hi Jennifer.

I tried both your suggestions and they worked. I see what .toLocaleString does. It makes large numbers more readable. Nice idea!

Thank you for your help. It was very enlightening.

Sean