Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
In this practice session, you'll use a loop to create a number guessing game.
Resources
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
[MUSIC]
0:00
Hi everyone, Guil here.
0:08
In this practice session you'll build
up your JavaScript skills by practicing
0:10
the basics of loops.
0:14
You've learned that loops
provide a quick and
0:15
easy way to repeat a block of
JavaScript code multiple times.
0:17
And JavaScript supplies several
statements for creating loops.
0:20
So far you've learned about statements
like while, do while, and for.
0:23
In this session, you're going to use
a loop to create a number guessing game
0:27
that matches a user against the computer.
0:31
When the page loads,
a prompt dialog box appears.
0:34
The user has to try to guess
the number between 1 and 10.
0:37
The same prompt appears again and again
until the user guesses the correct number.
0:41
Once they do,
a message displays on the page.
0:46
To get started,
launch the workspace with this video, or
0:49
download the project files and
use your preferred text editor.
0:51
This is a multi-step challenge.
0:54
And in the file app.js, I've included
comments providing the instructions you'll
0:56
need to follow to complete
the first part of this challenge.
1:01
Notice that there's already
code in this JavaScript file.
1:04
The handy getRandomNumber function you've
worked with in previous courses and
1:07
a few variables to get you started.
1:12
First, we get a reference
to the main element in
1:14
index.html and
assign it to the variable main.
1:17
Then we get a random
number from 1 to 10 and
1:23
assign it to the variable randomNumber.
1:25
Next, there's a variable named guess.
1:28
This variable is going to get updated
with the player's guess each time your
1:30
loop runs.
1:34
Now let's go over what you'll need to do.
1:36
You'll use a loop to create
a number guessing game.
1:38
First, you'll need to ask the user for
some input.
1:42
Each time the loop runs,
1:46
a prompt dialog should open that asks the
user to guess a number between 1 and 10.
1:47
The value they enter gets
assigned to the guess variable.
1:52
The condition should end the loop when the
user's guess matches the random number.
1:56
Then you'll display a message on the page,
2:02
letting the user know that
they guessed the number.
2:04
I recommend using either a while or
do while loop for this challenge.
2:07
You'll also get a chance to write a for
loop later in this practice session.
2:12
This practice exercise is a great way
to help reinforce what you've learned
2:16
about working with loops.
2:20
So good luck, have fun.
2:22
And in the next video, I'll show you one
possible solution to this challenge.
2:24
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