**Heads up!** To view this whole video, sign in with your Courses account or enroll in your free 7-day trial.
Sign In
Enroll

Start a free Courses 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

All right now I will show you how I solved the second part of this challenge. 0:00 The program should ask for two numbers and provide a random number between the two. 0:04 First, I need to ask for another input. 0:09 I will declare a variable named inputLow and assign it the prompt method. 0:11 I'll pass it the string "Please provide your lowest number". 0:18 Then change the input high message to Please provide your highest number. 0:25 The string value stored and input low needs to be converted into a number type. 0:33 I'll store that number value in a variable named lowNumber. 0:39 I'll again use the parseInt method to convert the string to an integer. 0:44 The variable lowNumber represents the lowest possible random number. 0:53 So now I need to change how the random number gets generated. 0:58 For example, instead of being a number from 1 to the value stored in highNumber, 1:01 it needs to generate a number from lowNumber to highNumber. 1:07 To achieve this, I'll change the number being multiplied by Math.random. 1:11 I'll subtract the lowNumber from the highNumber and 1:16 add 1 to it with highNumber- lowNumber + 1. 1:21 And then add the lowNumber to the return value of Math.floor. 1:26 So this generates a number from 0 to the difference between the highest and 1:34 lowest number plus 1. 1:39 For example, if I want a random number from 10 to 25. 1:41 Well, 25 minus 10 plus 1 is 16. 1:45 So this would generate a random number from 0 up to but not including 16. 1:50 By providing that value to Math.floor, I'll get a random number from 0 to 15. 1:56 Then, I add the lowest number which produces the number from 10 to 25. 2:02 Now, don't worry at all if the math still seems confusing. 2:07 At least now you have an equation that does the math for you. 2:10 Next, I'll update the random number console message by inserting the value of 2:14 the low number variable into the string. 2:18 And I'll update the message in the else clause to You need to provide two numbers. 2:25 Try again. 2:30 Finally, I'll update the condition and the if statement. 2:34 This time I need to check if the values stored in lowNumber and 2:38 the value in highNumber is a true number. 2:43 In other words, make sure that both are not Nan or not a number. 2:46 I'll use the logical and operator, 2:50 you learned about in a previous course in the condition like this. 2:52 LowNumber && highNumber. 2:56 If even one of these is not a number, 3:00 the entire condition evaluates the false and the code in the S clause runs. 3:03 Okay, let's see how this works now. 3:09 I'll save my file, refresh the page. 3:12 I'll enter the lowest number, let's say 10 and the highest number 25. 3:15 The console outputs the message 14 is a random number between 10 and 25. 3:22 Now, I'll try entering a value like three then 15. 3:28 The console outputs, You need to provide two numbers. 3:34 Try again, good. 3:37 Nice work with the addition of numeric data types and 3:42 the math object to your toolset, 3:44 you're willing your way to programming some pretty powerful applications. 3:46 To practice more with numbers and math, 3:51 be sure to review the resources posted in the teachers notes. 3:53 And we're here to help. 3:56 So if you have questions about anything covered in this course, 3:57 feel free to reach out to other students in the community or the treehouse staff. 4:00 Thanks everyone and happy coding. 4:04

You need to sign up for Treehouse in order to download course files.

Sign up