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
Update the random number program to ask for two numbers, then provide a random number between the two.
What if the value of lowNumber
is 0
?
The number zero is considered a "falsy" value in JavaScript. In other words, if the value of lowerNumber
is 0
, the if
condition evaluates to false
and the code in the else
clause runs.
To make the number 0
an acceptable lowNumber
value in the random number program, use the 'greater than or equal to' operator (>=
) in the condition:
if ( lowNumber >= 0 && highNumber ) {
...
} else {
...
}
Another approach using isNaN()
JavaScript provides a special function called isNaN()
(or "is not a number") that takes one argument and returns a boolean value. It returns true
if the value is NOT a number, and false
if it is.
isNaN()
returns the value true
. However, passing it an actual number, like 6
, returns false
.
In this case, you first test if EITHER variable is not a number. If even one is not a number, display the "Try again" message. To test both variables, use the logical OR (
) operator:
// Convert the input to a number
const lowNumber = parseInt(inputLow);
const highNumber = parseInt(inputHigh);
// Check if lowNumber OR highNumber is not a number
if ( isNaN(lowNumber)  isNaN(highNumber) ) {
console.log('You need to provide two numbers. Try again.');
} else {
// Use Math.random() and the user's number to generate a random number
const randomNumber = Math.floor( Math.random() * (highNumber  lowNumber + 1) ) + lowNumber;
// Create a message displaying the random number
console.log(`${randomNumber} is a random number between ${lowNumber} and ${highNumber}.`);
}
Resources

0:00
All right now I will show you how I solved the second part of this challenge.

0:04
The program should ask for two numbers and provide a random number between the two.

0:09
First ¿, I need to ask for another input.

0:11
I will declare a variable N¿named inputLow and assign it the prompt method.

0:18
I'll pass it the string Please provide your lowest number.

0:25
Then change the input high message to Please provide your highest number.

0:33
The string value stored and input low needs to be converted into a number type.

0:39
I'll store that number value in a variable named lowNumber.

0:44
I'll again use the parseInt method to convert the string to an integer.

0:53
The variable lowNumber represents the lowest possible random number.

0:58
So now I need to change how the random number gets generated.

1:01
For example, instead of being a number from 1 to the value stored in highNumber,

1:07
it needs to generate a number from lowNumber to highNumber.

1:11
To achieve this, I'll change the number being multiplied by Math.random.

1:16
I'll subtract the lowNumber from the highNumber and

1:21
add 1 to it with highNumber lowNumber + 1.

1:26
And then add the lowNumber to the return value of Math.floor.

1:34
So this generates a number from 0 to the difference between the highest and

1:39
lowest number plus 1.

1:41
For example, if I want a random number from 10 to 25.

1:45
Well, 25 minus 10 plus 1 is 16.

1:50
So this would generate a random number from 0 up to but not including 16.

1:56
By providing that value to Math.floor, I'll get a random number from 0 to 15.

2:02
Then, I add the lowest number which produces the number from 10 to 25.

2:07
Now, don't worry at all if the math still seems confusing.

2:10
At least now you have an equation that does the math for you.

2:14
Next, I'll update the random number console message by inserting the value of

2:18
the low number variable into the string.

2:25
And I'll update the message in the S clause to You need to provide two numbers.

2:30
Try again.

2:34
Finally, I'll update the condition and the if statement.

2:38
This time I need to check if the values stored in lowNumber and

2:43
the value in highNumber is a true number.

2:46
In other words, make sure that both are not Nan or not a number.

2:50
I'll use the logical and operator,

2:52
you learned about in a previous course in the condition like this.

2:56
LowNumber && highNumber.

3:00
If even one of these is not a number,

3:03
the entire condition evaluates the false and the code in the S clause runs.

3:09
Okay, let's see how this works now.

3:12
I'll save my file, refresh the page.

3:15
I'll enter the lowest number, let's say 10 and the highest number 25.

3:22
The console outputs the message 14 is a random number between 10 and 25.

3:28
Now, I'll try entering a value like three then 15.

3:34
The console outputs, You need to provide two numbers.

3:37
Try again, good.

3:42
Nice work with the addition of numeric data types and

3:44
the math object to your toolset,

3:46
you're willing your way to programming some pretty powerful applications.

3:51
To practice more with numbers and math,

3:53
be sure to review the resources posted in the teachers notes.

3:56
And we're here to help.

3:57
So if you have questions about anything covered in this course,

4:00
feel free to reach out to other students in the community or the treehouse staff.

4:04
Thanks everyone and happy coding.
You need to sign up for Treehouse in order to download course files.
Sign up