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
In the previous video, Dave challenged you to use Workspaces to create a random number generate. In this video, Dave will show you one way to solve his Random Number Challenge.
Did you try the Random Number Generator Challenge?
If you didn't already, try the Random Number Challenge now!
Launch Workspaces to try The Random Challenge
Using Workspaces, create a Random Number Generator
See if you can build a program that collects a number from a user, then prints out a random number from 1 to the number the user selects. Now is the opportunity to practice using some of the commands you learned in this course.
Incorporate some of the commands you've learned so far:

prompt()
collect user input 
parseInt()
convert the input to an integer 
Math.random()
create a random number
Share your Workspace in the Treehouse Community

0:00
How did you do?

0:00
Were you able to complete both challenges?

0:03
Let me show you how I did it.

0:05
First, I'll create a new file named random.js and

0:10
open the index.html file and attach the script to it inside the body.

0:17
Remember the first step of this challenge was collecting a number from a visitor.

0:21
I'll start by creating a variable and

0:23
storing input from the user inside that variable.

0:28
Input you receive from the prompt method is always a string.

0:32
Even if someone types a number into the dialog box.

0:35
We should convert that value into a true number.

0:38
We can use the parseInt command to do that.

0:41
We'll store that into a variable named topNumber to represent the highest

0:45
possible random number.

0:49
Now we can create a random number from one to whatever the visitor typed in.

0:53
We'll store that in a variable named randomNumber.

1:01
Remember that the math.random method generates a number from zero up to but

1:07
not including one.

1:08
Something like 0.567899 for instance.

1:11
You can multiply that by the top number in the range, for

1:15
example six if you wanted to simulate a die roll of one to six.

1:20
Then you'll get a floating point number or

1:22
a number with a decimal in it like 3.407394.

1:29
The Math.floor method then drops that number to the nearest integer, three,

1:33
for example.

1:35
Each time this code runs you'll get a number from zero to five.

1:40
So, to get a number from one to six, you'll have to add one to this.

1:46
Now I'll construct the message.

1:48
I want it to look something like this, where four is the random number,

1:54
and six is the number supplied by the visitor.

1:57
I'll start by creating a variable to hold this message.

2:02
And then build up the string by changing these numbers four and six into variables.

2:10
[BLANK_AUDIO]

2:15
Lastly, I'll write this to the web page.

2:19
Let's see how it works.

2:20
I'll save this file and the index.html file and then preview the workspace.

2:27
If I reload this page a few times you'll see that I get a different random number

2:32
almost every time, great.

2:35
Now for the second part of this challenge.

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

2:43
First, you just need to ask for another input number and parse the value.

2:49
[BLANK_AUDIO]

2:56
We also need to change how the random number is generated.

2:59
Instead of being a number from one to z, we need to make it a number from x to z.

3:05
So, we'll start by changing the number we multiply the random method by.

3:13
And then we add the bottom number to the results.

3:17
Basically, this generates a number from zero to the difference between the top and

3:21
bottom range plus one.

3:23
So, for example, if we wanted a random number from ten to 25,

3:27
this generates a random number from zero up to but not including 16.

3:32
That's because 25 minus ten plus one is 16.

3:35
When you add the Math.floor function, we get a random number from zero to 15.

3:42
Then we take that and add the bottom number,

3:46
in other words a random from ten, zero plus ten.

3:51
To 25, 15 plus ten. Don't worry if you don't

3:54
understand the math, because now you have an equation that does the math for you.

3:59
We can also update the message by adding the start number variable there as well.

4:06
Okay.

4:07
I'll save this file and preview the workspace.

4:11
Lets see how it works.

4:15
Great!

4:16
You may have already tried this but

4:18
what if you provide a string instead of a number or a word?

4:22
It doesn't work out that well.

4:24
What we need is a way to test to see if a user typed in something that was a number.

4:29
To do that we need to use a new programming concept called

4:33
conditional statements.

4:34
I'll teach you all about those in the next stage.
You need to sign up for Treehouse in order to download course files.
Sign up