Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
Build a short program that helps you calculate the number of seconds in a day, hours in a week, or minutes in a year.
Resources
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
All right,
let's do some math with JavaScript.
0:00
You'll build a simple program that can
help you calculate the number of seconds
0:02
in a day, hours in a week,
or minutes in a year.
0:06
You can code along with me by launching
the workspace with this video.
0:09
Open the math.js file
located inside the js folder.
0:13
The file is already linked to index.html.
0:17
Let's start by creating a few variables
that hold information about time.
0:23
First, declare a variable
named seconds per minute,
0:28
that stores the number of
seconds in a minute, 60.
0:32
Then declare for more variables for
other length of time.
0:36
First minsPerHour = 60,
0:40
then const hoursPerDay = 24,
0:46
and const daysPerWeek = 7,
0:53
and const weeksPerYear = 52.
0:59
With this information,
we can now start to do some math.
1:06
Let's put a message to the console that
lists the number of seconds in a day.
1:10
I'll type a set of backticks
to create a template literal
1:15
that displays the message,
there are dollar sign curly braces
1:18
secondsPerDay seconds in a day.
1:23
We can calculate the number of seconds in
a day by multiplying the number of seconds
1:32
in a minute by the number of minutes in
an hour and the number of hours in a day.
1:37
Above console.log, I'll declare
the variable secondsPerDay and
1:44
assign it secondsPerMinute times
minsPerHour times hoursPerDay.
1:51
Now the variable secondsPerDay holds
the number of seconds in a day.
1:59
As you can see in the console,
there are 86,400 seconds in a day.
2:05
Here's a small assignment for you.
2:11
Add another variable named yearsAlive and
assign it your age.
2:13
Then print a second message to the console
by adding another console.log statement.
2:19
The message should say,
I've been alive for more than x seconds.
2:25
Replace the x with the number of seconds
that have elapsed in all the years you've
2:38
been alive.
2:42
You'll need to do some multiplication
of variables to get it done.
2:43
So good luck, and
2:46
when you're done, why don't you share your
solution with other Treehouse students?
2:47
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up