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
Learn one possible solution for tracking guess attempts and displaying the number of attempts on the page.
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
Hey, how'd you do?
0:00
Okay, now I'll show you how I would
keep track of guess attempts and
0:01
display the number of
attempts on the page.
0:04
Back in my program, I'll declare
a new variable named attempts and
0:07
set it to 0 initially.
0:12
Then down in the do block,
I'll increase the value of
0:15
attempts by 1 each time
it runs with attempts++.
0:20
I'm using the increment operator to add 1
to the current number value stored in
0:25
attempts each time the loop runs.
0:29
It's totally fine if you use the addition
assignment operator here too.
0:32
This works exactly the same.
0:35
Outside of the loop,
within the template literal,
0:37
I'll let the user know
how many attempts it took
0:43
to guess the correct
number by changing this
0:48
message to it took you
${attempts} tries to
0:53
guess the number, ${randomNumber}.
0:57
I'll save my file and
back in the browser, enter my guesses.
1:05
And after three attempts,
1:11
I get the message it took you 3 tries
to guess the number 3, awesome.
1:13
All right, time for
the final part of this challenge.
1:18
Let's limit the number of
guests attempts to 10.
1:21
I'll do that by changing
the condition here to attempts < 10.
1:24
Now the user has 10 attempts
to guess the random number.
1:29
The loop starts counting from 0 attempts,
and
1:33
when the value assigned to attempts is
no longer less than 10, the loop ends.
1:37
Notice how the loop continues to run and
1:43
keeps asking the user to guess the correct
number until it has run 10 times.
1:46
Even if they've already entered
a number that matches the random number
1:51
they have to wait until the loop ends.
1:54
When the numbers match,
the loop should immediately end and
1:57
the success message should
display on the page.
2:00
So for this next part of the challenge,
you'll need to provide a way to escape or
2:03
break out of this loop.
2:07
The loop will either run 10 times, or if
the user guesses the correct number first,
2:09
the loop ends and
the rest of the program continues.
2:14
In the loop,
2:19
I'll add the comment Terminate loop
if the user guesses the number.
2:20
You'll also need to consider what happens
if a user does not get the number
2:31
after 10 tries.
2:36
For instance, if the user guesses
the number, end the loop and
2:37
display the success message.
2:41
Else display a message on the page letting
them know that they did not guess the number
2:43
and reveal what the number was.
2:48
Up next,
I'll show you how I would solve this.
2:51
Good luck.
2:53
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