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 trialFrederik Dobbener
2,356 PointsHi, i can't quite figure out what's missing with my program.
const main = document.querySelector('main');
const randomNumber = getRandomNumber(10);
let guess;
function getRandomNumber(upper) {
return Math.floor( Math.random() * upper ) + 1;
}
do {
// ask for a number and assign the answer to the variable 'guess'
guess = prompt('Enter a number between 1 and 10.');
// perform a string to number conversion with 'guess'
guess = parseInt(guess);
// verify that 'guess' is a number and between 1-10
// if any of these conditions are true, prompt the user to retry
if ( isNaN(guess) || guess > 10 || guess < 0 ) {
// ask for a number and assign the answer to the variable 'guess'
guess = prompt('Only a number between 1 and 10 is acceptable. Please try again.');
// perform a string to number conversion with 'guess
parseInt(guess);
// else if 'guess' is equal to the random number, then end the loop and display a message
} else if ( guess === randomNumber ) {
main.innerHTML = `<h1>Congratulations! You guessed the random number!<h2>`;
}
}
while ( guess !== randomNumber ) {
// ask for a number and assign the answer to the variable 'guess'
guess = prompt('Enter a number between 1 and 10.');
// perform a string to number conversion with 'guess'
guess = parseInt(guess);
}
Cody Hansen
5,517 PointsI wrote some HTML to match and copied your code over and it seems to work fine for me! Would you mind posting the HTML as well?
EDIT: Looks like a was late by a second, Brandon! Sorry for repeating your answer!
1 Answer
Frederik Dobbener
2,356 PointsThank you all for replying. It seemed to work only once and after that i kept entering the same number for like a hundred times into the prompt window and i never got a successful guess out of it again
Brandon White
Full Stack JavaScript Techdegree Graduate 34,662 PointsBrandon White
Full Stack JavaScript Techdegree Graduate 34,662 PointsHi Frederick,
What bug/issue are you experiencing when you run your code? I ran your code, and it worked just fine for me.