Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialVictor Stanciu
Full Stack JavaScript Techdegree Student 11,196 PointsPlease help me understand how my code works.
Hi!
I don't understand why the code inside the else
clause is not executed immediately when the number is NOT guessed. I was expecting it to display the message Too bad! You did not guess the number ${randomNumber}!
AND ONLY AFTER displaying it, continue with the prompt
(from the next couple of lines, outside the if-else
).
My code is written below.
Thanks a lot for your help!
const main = document.querySelector('main');
const randomNumber = getRandomNumber(10);
let guess = prompt('I\'m thinking of a number between 1 and 10. Guess the number!');
let attempts = 1;
console.log(`The random number is ${randomNumber}`); // Just to help understand how the code works
console.log(`The number of attempts is ${attempts}`); // Just to help understand how the code works
// Keep track of the number of guess attempts
function getRandomNumber(upper) {
return Math.floor( Math.random() * upper ) + 1;
}
// TODO: Use a loop to create a number guessing game
// 1) Ask the user to enter a number and assign that value to the `guess` variable
// 2) End the loop when the user's guess matches the random number
// 3) Display a message letting the user know they guessed the number
while(attempts < 10){
if(+guess === randomNumber){
document.querySelector('main').innerHTML = `<h1>It took you ${attempts} tries to guess the number ${randomNumber}!</h1>`;
break;
} else {
// WHY DOES THIS NOT EXECUTE IMMEDIATLY AFTER THE IF EVALUATES TO FALSE?
document.querySelector('main').innerHTML = `<h1>Too bad! You did not guess the number ${randomNumber}!</h1>`;
}
guess = prompt(`Try again!`);
attempts++;
console.log(`The number of attempts is ${attempts}`); // Just to help understand how the code works
}
1 Answer
Steven Parker
231,275 PointsThis is covered in the "Teacher's Notes" section in one of the course videos (but not this practice session). Modern browsers do not update the page until after the program ends. While in the loop, the only immediate forms of output are alert and prompt pop-ups, and console logs.
Victor Stanciu
Full Stack JavaScript Techdegree Student 11,196 PointsVictor Stanciu
Full Stack JavaScript Techdegree Student 11,196 PointsHi, Steven!
Thanks for the help! I'll try and find the information in the "Teacher's Notes". Usually I read them first, including the documentation linked in that section. Must've missed something!