Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Write four JavaScript functions to calculate areas and volumes.
Course
Related Videos
Mozilla Developer Network Reference

0:00
[MUSIC]

0:04
This practice session covers basic JavaScript functions.

0:07
It's a great followup to the fifth part of the JavaScript Basics course.

0:11
If you haven't taken that yet, and find this practice session too difficult,

0:15
then go through the first five parts of that course first.

0:18
I've added a link in the teacher's notes below.

0:20
For this practice session, you'll write four functions.

0:23
Call those functions and output the results to the console.

0:27
It's pretty simple but combines the basics of creating and

0:30
using JavaScript functions.

0:31
I've attached a workspace to this video which includes some starter files and

0:35
instructions.

0:36
Open up the workspace and open up the geometry.js file.

0:39
You'll see six instructions listed as JavaScript comments.

0:43
Here's what you should do.

0:45
First attach the JavaScript file to the index.html file.

0:48
After all, the program won't work at all unless the web page loads it.

0:52
Next create a function that calculates the area of a rectangle.

0:56
To calculate the area of a rectangle, you multiply the width by the height.

1:01
So this function needs to accept two arguments,

1:04
do some math, then return the results back.

1:06
Then you'll create another function to calculate the volume

1:09
of a rectangular prism.

1:11
Also known as a box.

1:13
For that, you multiply the width by the height by the length.

1:16
This function should return the volume.

1:19
Now the next two functions involve calculating the area of a circle and

1:23
the volume of a sphere.

1:24
These both require using the JavaScript math object to get the value of pi.

1:29
And to use a method that lets you raise the number to a certain power.

1:33
For example x to the second power or x squared.

1:37
Now I didn't cover those in JavaScript basics course, so

1:40
you'll need to do a bit of research to figure out how to use them.

1:43
I've included links in the teacher's notes below.

1:46
Okay so if you have the radius of a circle to calculate the area of a circle,

1:51
you take pi times the radius squared.

1:54
Squared is also refered as the second power or to the second power.

1:58
So the function will receive a radius value and return the area.

2:02
The last function will calculate the volume of a sphere.

2:05
To do that you multiply four thirds by pi, by the radius cubed or

2:09
the radius to the third power.

2:11
Okay now finally you'll add four lines to log out

2:15
the results of your function to the JavaScript console.

2:19
It's not super exciting to look at, but this is what your output should look like,

2:23
something like this.

2:24
Now if you wanna do some quick study before tackling this practice session.

2:27
In the teacher's notes look for

2:29
links to videos that can help you solve this challenge.

2:31
But if you're ready, go for it.

2:33
Program your solution and I'll show you my solution in the next video.
You need to sign up for Treehouse in order to download course files.
Sign up