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

How to calculate the numbers of seconds I've been alive?

Hi, I I am not entirely sure how to calculate the number of seconds I've been alive for? Would it be

  var secondsPerMin = 60;
  var minsPerHour = 60;
  var hoursPerDay = 24;
  var daysPerWeek = 7; 
  var weeksPerYear = 52
  var yearsAlive = 25; 
  var secondsAlive = secondsPerMIn * minsPerHour * hoursPerDay * daysPerWeek * weeksPerYear;

alert("I've been alive for more than " + secondsAlive + ' seconds');

I don't think that is right but how do you know what to calculate in order to get the right answer? What if I wanted to calculate the number of days until my next birthday as mentioned in the video? How would go about performing those calculations?

Sorry. Math isn't my strong suit

Gabriel de leon
Gabriel de leon
1,611 Points

What you were actually missing was just one multiplication. You needed to add a multiplication for yearsAlive;

var secondsAlive = secondsPerMIn * minsPerHour * hoursPerDay * daysPerWeek * weeksPerYear * yearsAlive;

That would have given you the amount of seconds in a year.

6 Answers

Hi Louis,

I learned these kind of concept in following way:

  1. When you want to go from smaller unit to a greater unit, you divide.

I have 3500 grams and I want to know how many Kilograms it's ( I do know that 1 kg = 1000 g) , so as I am going from small (gram) to greater (kilogram) I will divide 3500 / 1000 = > which will give be 3.5 kg.

Now same example with Time, I need to convert 5600 seconds (small) to minutes (large) , so I will again divide 5600 with the number of seconds in a minute which are 60 .. so 5600/60 => 93.33 minutes and then 93.33 / 60 => 1.5 hour

So , 5600 seconds are equal to 93.33 minutes and 1.5 hour.

  1. When you want to go from greater unit to smaller unit, you multiply.

How many seconds in 1.5 hour?

1.5 * 60 (minutes in an hour) = > 93.33 mins

93.33 * 60 (seconds in a minute) => 5600 seconds

Now to answer your query about how many seconds you have been alive , let's see that:

How old are you ? 25 years.

Years -> weeks => 25*52 = 1300 weeks

weeks -> days => 1300*7 = 9100 days

days -> hours => 9100 * 24 = 218,400 hours

hours -> minutes => 218,400 * 60 = 13,104,000 mins

minutes -> seconds => 13,104,000 * 60 = 786,240,000 seconds

Wow that's a lot of seconds :D

I hope it clears your concept of conversion from one smaller unit to a greater unit and vice versa.

Happy learning :)

  • Zain

Hey Zain!

Thank you for taking the time to post this. It really helped me out a lot.

Riaz Khan
Riaz Khan
5,320 Points

awesome explanation!

Good explanation of the logic behind that simple mathematics.

Alexandra Edwards
Alexandra Edwards
4,686 Points

I just realized I added this answer as a comment by accident. Oops, Treehouse Newbie mistake. Here was what I had that was in the wrong spot:

You're so close! How about making the corrections in the comment line below:

var secondsPerMin = 60;
var minsPerHour = 60;
var hoursPerDay = 24;
var daysPerWeek = 7;
var weeksPerYear = 52
var yearsAlive = 25;

// lower case 'i' on secondsPerMin and multiply the variable yearsAlive after weeksPerYear
var secondsAlive = secondsPerMIn * minsPerHour * hoursPerDay * daysPerWeek * weeksPerYear;            

alert("I've been alive for more than " + secondsAlive + ' seconds');

I have finally figured it out

var secondsPerMin = 60; var minsPerHour = 60; var housPerDay = 24; var daysPerWeek = 7; var weeksPerYear = 52; var yourAge = 27;

var secondsPerDay = secondsPerMin * minsPerHour * housPerDay; document.write('There are ' + secondsPerDay + ' seconds in a day. '); var secondsPerWeek = secondsPerDay * daysPerWeek; var secondsPerYear = secondsPerWeek * weeksPerYear; var yearsAlive = secondsPerYear * yourAge; document.write('<h2>I have been alive for more than ' + yearsAlive + ' seconds.</h2>');

Soon Han Ooi
PLUS
Soon Han Ooi
Courses Plus Student 1,062 Points

What is the easiest way to add photos here? I would like to post my answer here too.

Here's my calculation.

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

var secondsPerDay = secondsPerMin * minsPerHour * hoursPerDay;
var secondsPerWeek = secondsPerDay * daysPerWeek;
var secondsPerYear = secondsPerWeek * weeksPerYear
var yearsAlive = secondsPerYear * yourAge;
document.write(`<h2>I have been alive for ${yearsAlive} seconds.</h2>`);

This was my code that I went with

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


var secondsPerDay = secondsPerMin * minsPerHour * hoursPerDay;
var secondsPerWeek = secondsPerDay * daysPerWeek;
var secondsPerYear = secondsPerWeek * weeksPerYear;
var yearsAlive = secondsPerYear * 28;

document.write("There are " + secondsPerDay + " seconds in a day </br>");
document.write('There are ' + secondsPerWeek + ' seconds per week </br>');
document.write('There are ' + secondsPerYear + ' seconds per year </br>');
document.write('I\'ve been alive for more than ' + yearsAlive + ' seconds');