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 Practice JavaScript Loops!
You have completed Practice JavaScript Loops!
Preview
This video covers one solution to the first part of the "Number Guessing Game" challenge.
Resources
while loop solution
Review how you might write the number guessing program using a while loop.
const main = document.querySelector('main');
const randomNumber = getRandomNumber(10);
let guess;
function getRandomNumber(upper) {...}
while ( parseInt(guess) !== randomNumber ) {
guess = prompt('I am thinking of a number between 1 and 10. What is it?');
}
main.innerHTML = `<h1>You guessed the number! It was ${randomNumber}.</h1>`;
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
The goal was to ask the user to guess
the number from one to 10 again and
0:00
again until their guests match the number
assigned to the randomNumber variable.
0:00
Now I'll show you my solution which
you can compare to what you wrote.
0:05
You can also reference all my code
when you download the project files.
0:09
I chose to use a do...while loop.
0:13
So I'll start by adding
a do while statement.
0:16
Everything you add inside this
do block is the loop itself, and
0:22
it's going to run at least once.
0:27
In the do block,
I'll first collect the player's guess.
0:29
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