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 trialSean Flanagan
33,235 PointsMy 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
Treehouse TeacherHi 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!
Sean Flanagan
33,235 PointsHi 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
Sean Flanagan
33,235 PointsSean Flanagan
33,235 PointsHi Jennifer.
So instead of multiplying yearsAlive by secondsPerDay, I should have multiplied yearsAlive by secondsPerYear?
Thanks
Sean
Jennifer Nordell
Treehouse TeacherJennifer Nordell
Treehouse TeacherIn 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 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?
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.