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

Is this correct to find seconds alive???

var secondsPerMin = 60;
var minsPerHour = 60;
var hoursPerDay = 24;
var daysPerWeek = 7;
var weeksPerYear = 52;
var secondsPerDay = secondsPerMin * minsPerHour * hoursPerDay;

//document.write(secondsPerDay);

var yearsAlive = 23 * weeksPerYear * daysPerWeek * hoursPerDay * secondsPerMin;

document.write(yearsAlive);

5 Answers

what about?

var yearsAlive = 20;
var secondsAlive = secondsPerMin * minsPerHour * hoursPerDay * daysPerWeek * weeksPerYear * yearsAlive;
document.writeln('I have been alive ' + secondsAlive + ' total seconds.');
landon williams
landon williams
3,900 Points

This is the correct way

var yearsAlive = 30;

var secondsAlive = 365 * secondsPerDay * yearsAlive;

document.write('I have been alive ' + secondsAlive + ' seconds!');

Hugo Paz
Hugo Paz
15,622 Points

Hi Janelle,

It will give you a rough estimate, you are not taking into account leap years, the date the person was born and the current date.

I would also use your secondsPerDay variable and then multiply it by 365, or 366 if its a leap year.

So you need to know the start year and the end year, see how many leap years there was and multiply accordingly.

Say the starting year is 1978, there were 9 leap years till now, 1980, 1984, 1988, 1992, 1996, 2000, 2004, 2008 and 2012.

So of 36 years, 9 were leap years, so you do 36-9 = 27.

(27 * 365 * secondsPerDay) + (9 * 366 * secondsPerDay) - ((days till the birthday + days remaining in current year)*secondsPerDay)

Samuel Webb
Samuel Webb
25,370 Points

You would also need to take into account how many days have passed since your birthday. Then add the total seconds of that, to your time alive as well.

Very through thank you!

I forgot about leap years! Since we are being thorough, let's not forget about Leap Seconds - seriously. Although, I'm going to not include this in my answer I will include leap years - thanks for the reminder/info about that.

If you want to know about Leap Seconds, here are 2 quick reads and then the 3rd is how Google has it all figured out (of course :-) !

1) https://en.wikipedia.org/wiki/Leap_second 2) http://money.cnn.com/2015/01/13/technology/leap-second/

3) http://googleblog.blogspot.com/2011/09/time-technology-and-leaping-seconds.html

-Travis

Jeff Martinez
Jeff Martinez
1,826 Points

How do you add commas in the answer?

Amber Taal
Amber Taal
1,666 Points

I tried the code below but when I go to the workspace preview, it doesn't show anything. What am I doing wrong?

<p>
var secondPerMin = 60;
var minsPerHour = 60;
var hoursPerDay = 24;
var daysPerWeek = 7;
var weeksPerYear = 52;
var secondsPerDay = secondsPerMin * minsPerHour * hoursPerDay;
document.write(β€˜There are β€˜ + secondsPerDay + β€˜ seconds in a day’);

var yearsAlive = prompt ("How many years have you been alive?");
var secondsAlive = 365 * secondsPerDay * yearsAlive;
document.write('I have been alive ' + secondsAlive + ' seconds!');
</p>
Dylan Cairns
Dylan Cairns
11,191 Points

Bit of a late response but I just got to this video.

You have a few syntax errors which is why it's not working. In line 6 you include the variable SecondsPerMin instead of what you declared at top: SecondPerMin For your first document.write you have to use single or double quotes ' or " not back ticks. For your prompt you shouldn't have a space before the parenthesis it should be *prompt("test here");

Here's what I changed it to for it to work:

var secondPerMin = 60;
var minsPerHour = 60;
var hoursPerDay = 24;
var daysPerWeek = 7;
var weeksPerYear = 52;
var secondsPerDay = secondPerMin * minsPerHour * hoursPerDay;
document.write('<h1>There are ' + secondsPerDay + ' seconds in a day. ');

var yearsAlive = prompt("How many years have you been alive?");
var secondsAlive = 365 * secondsPerDay * yearsAlive;
document.write('I have been alive ' + secondsAlive + ' seconds!</h1>');