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 Numbers!
You have completed JavaScript Numbers!
Preview
JavaScript lets you create random numbers, which you can use to add variety and surprise in your programs. You generate a random number in JavaScript via a method named Math.random().
Resources
Example Return Values
| Math.floor() | Return Value | Math.ceil() | Return Value |
|---|---|---|---|
Math.floor() |
Math.ceil() |
||
Math.floor(1.2) |
1 |
Math.ceil(1.2) |
2 |
Math.floor(0.0012) |
0 |
Math.ceil(0.0012) |
1 |
Math.floor(5.028) |
5 |
Math.ceil(5.028) |
6 |
Math.floor(2) |
2 |
Math.ceil(2) |
2 |
Math.floor(0) |
0 |
Math.ceil(0) |
0 |
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
You learn that JavaScript lets
you create random numbers,
0:00
which you can use to add variety and
surprise in your programs.
0:03
For example, you could program a game
to randomly place enemy spaceships and
0:06
asteroids on the screen.
0:10
Or you can liven up your homepage by
randomly selecting a photo to display each
0:12
time someone visits your site.
0:16
Or, randomly select a question
as part of a quiz application.
0:17
Each of these tasks starts
with creating a random number.
0:22
Generating a random number in
JavaScript is quite straightforward.
0:26
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