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
Write four JavaScript functions to calculate areas and volumes.
Course
Related Videos
Mozilla Developer Network Reference
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
[MUSIC]
0:00
This practice session covers
basic JavaScript functions.
0:04
It's a great followup to the fifth
part of the JavaScript Basics course.
0:07
If you haven't taken that yet, and
find this practice session too difficult,
0:11
then go through the first five
parts of that course first.
0:15
I've added a link in
the teacher's notes below.
0:18
For this practice session,
you'll write four functions.
0:20
Call those functions and
output the results to the console.
0:23
It's pretty simple but
combines the basics of creating and
0:27
using JavaScript functions.
0:30
I've attached a workspace to this video
which includes some starter files and
0:31
instructions.
0:35
Open up the workspace and
open up the geometry.js file.
0:36
You'll see six instructions
listed as JavaScript comments.
0:39
Here's what you should do.
0:43
First attach the JavaScript
file to the index.html file.
0:45
After all, the program won't work at
all unless the web page loads it.
0:48
Next create a function that
calculates the area of a rectangle.
0:52
To calculate the area of a rectangle,
you multiply the width by the height.
0:56
So this function needs
to accept two arguments,
1:01
do some math,
then return the results back.
1:04
Then you'll create another
function to calculate the volume
1:06
of a rectangular prism.
1:09
Also known as a box.
1:11
For that, you multiply the width
by the height by the length.
1:13
This function should return the volume.
1:16
Now the next two functions involve
calculating the area of a circle and
1:19
the volume of a sphere.
1:23
These both require using the JavaScript
math object to get the value of pi.
1:24
And to use a method that lets you
raise the number to a certain power.
1:29
For example x to the second power or
x squared.
1:33
Now I didn't cover those in
JavaScript basics course, so
1:37
you'll need to do a bit of research
to figure out how to use them.
1:40
I've included links in
the teacher's notes below.
1:43
Okay so if you have the radius of a circle
to calculate the area of a circle,
1:46
you take pi times the radius squared.
1:51
Squared is also refered as the second
power or to the second power.
1:54
So the function will receive
a radius value and return the area.
1:58
The last function will calculate
the volume of a sphere.
2:02
To do that you multiply four thirds by pi,
by the radius cubed or
2:05
the radius to the third power.
2:09
Okay now finally you'll
add four lines to log out
2:11
the results of your function
to the JavaScript console.
2:15
It's not super exciting to look at, but
this is what your output should look like,
2:19
something like this.
2:23
Now if you wanna do some quick study
before tackling this practice session.
2:24
In the teacher's notes look for
2:27
links to videos that can help
you solve this challenge.
2:29
But if you're ready, go for it.
2:31
Program your solution and I'll show
you my solution in the next video.
2:33
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