Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed JavaScript Functions!
You have completed JavaScript Functions!
Preview
This video covers one solution to the function challenge.
This video doesn't have any notes.
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, now I'll walk you through how
I would write the random number function.
0:00
You learned how to create a random number
between two values in a previous course.
0:04
The difference is that now you're using
a function that accepts arguments.
0:08
For starters, I'll create
a function named getRandomNumber.
0:13
I'm using a function decoration, but
0:19
you could have written this as a function
expression or as an arrow function.
0:20
The function should produce a random
number value between two numbers.
0:25
So I need to add two
parameters to the function.
0:29
I'll call them lower and upper.
0:31
Next, I'll move the random number
code into the function and
0:36
place the parameters
where they need to go.
0:40
The upper value goes here, and
the lower value should go here and here.
0:43
I'll assign the result to
a variable named randomNumber,
0:55
Then return the value of randomNumber.
1:01
You could make this
code even more compact.
1:08
You don't need to assign
the formula to the variable,
1:11
and then return the variable.
1:14
Because the random number
generating code produces a value,
1:16
you can return its result, like this.
1:20
Let's look at why we can do this.
1:27
This code accepts information,
the lower and upper values,
1:29
and runs it through a couple of
JavaScript methods and does some math.
1:33
All of this code evaluates, as programmers
say, to a single value, a random number.
1:38
All a return statement
does is return a value.
1:44
It doesn't matter where
that value comes from.
1:47
It can be stored in a variable, or it can
be the result of another function, or
1:49
a mathematical operation,
as it is in this case.
1:52
All right, now to test this,
I'll call the function a few times and
1:57
print the results to the console.
2:00
First, I'll call getRandomNumber,
passing it the values 1 and 6.
2:05
Below that,
I'll call getRandomNumber again,
2:13
this time passing it 10 and 100.
2:17
And I'll call getRandomNumber a third
time, passing it the values 200 and 500.
2:20
I'll save my file, and
over in the console,
2:29
I get three random numbers, good.
2:34
So now I can use my random number
function in all sorts of ways.
2:37
For example,
display it in a message, like this.
2:41
Remember in a template literal,
2:56
you can include any JavaScript expression
between the dollar sign and curly braces.
2:57
And its value will be
inserted into the string.
3:02
By expression I mean any code
that evaluates to a single value,
3:05
like a variable, math operation, or
in this case, a call to a function.
3:09
In the console, we see the message,
41 is a random number between 10 and 100.
3:14
You might've added a default
parameter to your function too.
3:23
For example, set the upper parameter
to a default value of 100.
3:26
The getRandomNumber function
is working as expected.
3:42
However, there's at least one situation
in which the function won't work.
3:46
The two arguments passed to
the function must be numeric values.
3:50
If you, for example,
call the function with a string like six,
3:54
instead of the number 6,
the function will not work.
3:59
Notice the value NaN.
4:05
Part of being a good programmer is looking
for ways that your programs might break
4:07
and then coming up with a plan
to handle those problems.
4:12
To fix this, we can use an if statement
to make sure that both arguments
4:14
are numbers and then display an error if
one or both of the values are not numbers.
4:20
In the next video,
we'll implement the solution.
4:25
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