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 trialSean Flanagan
33,235 PointsWhy doesn't my program prompt you to do anything?
Hi.
I'm at a loss to understand why my program doesn't work. It doesn't prompt a guess. Here's my HTML:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="css/main.css">
<title>Random Number Guessing Game</title>
</head>
<body>
<div class="container">
<h1>Random Number Guessing Game</h1>
<script src="script.js">
</script>
</div>
</body>
</html>
And my JavaScript:
var correctGuess = false;
var randomNumber = Math.floor(Math.random() * 6 ) + 1;
var guess = prompt('I am thinking of a number between 1 and 6. What is it?');
if (parseInt(guess) === randomNumber ) {
correctGuess = true;
} else if ( parseInt(guess) < randomNumber ) {
var guessMore = prompt("Try again. The number I'm thinking of is more than " + guess);
if (parseInt(guessMore) === randomNumber) {
correctGuess = true;
}
} else if ( parseInt(guess) > randomNumber ) {
var guessLess = prompt("Try again. The number I'm thinking of is less than " + guess);
if (parseInt(guessLess) === randomNumber) {
correctGuess = true;
}
if ( correctGuess ) {
document.write('<p>You guessed the number!</p>');
} else {
document.write('<p>Sorry. The number was ' + randomNumber + '.</p>');
}
I'd be grateful for any help.
Sean
3 Answers
Amy Rutherford
9,659 PointsYou took the arrow and all caps I added to direct you to the error out, right? Just making sure! I should have commented it but I did it too quickly.
Amy Rutherford
9,659 PointsThe second else if statement is missing a closing bracket. When the conditional statement isn't closed, you get an Unexpected End of Statement error. I added some extra indentation and highlighted where the bracket should go. Once I added this, the prompt came up like it should.
var correctGuess = false;
var randomNumber = Math.floor(Math.random() * 6 ) + 1;
var guess = prompt('I am thinking of a number between 1 and 6. What is it?');
if (parseInt(guess) === randomNumber ) {
correctGuess = true;
} else if ( parseInt(guess) < randomNumber ) {
var guessMore = prompt("Try again. The number I'm thinking of is more than " + guess);
if (parseInt(guessMore) === randomNumber) {
correctGuess = true;
}
} else if ( parseInt(guess) > randomNumber ) {
var guessLess = prompt("Try again. The number I'm thinking of is less than " + guess);
if (parseInt(guessLess) === randomNumber) {
correctGuess = true;
}
} <---HERE IS THE BRACKET THAT WAS MISSING
if ( correctGuess ) {
document.write('<p>You guessed the number!</p>');
} else {
document.write('<p>Sorry. The number was ' + randomNumber + '.</p>');
}
Sean Flanagan
33,235 PointsHi Amy. I copied and pasted your JavaScript into my workspace. When I previewed it though, I got no prompt. I don't know what's happened.
Thanks
Sean :-)
Amy Rutherford
9,659 PointsThank you! It's the first time I've been able to answer a question for someone! I'm glad it helped.
David Tonge
Courses Plus Student 45,640 PointsThank you helping for helping your fellow student out!
Sean Flanagan
33,235 PointsSean Flanagan
33,235 PointsHi Amy. I hadn't, but I have now and it now works fine. Thanks for your help; it was really nice of you.
I've upvoted both your posts and given you a best answer for the second.
Sean :-)
Ayush Bahuguna
2,092 PointsAyush Bahuguna
2,092 PointsHi Mama, My JavaScript is
But, the prompt guess is not coming up.
Christopher Mlalazi
Front End Web Development Techdegree Graduate 17,305 PointsChristopher Mlalazi
Front End Web Development Techdegree Graduate 17,305 PointsWooo! I had the same curly bracket missing and I just couldn't see it until I decided to sift carefully through discussion, and you came to the rescue Amy! Thanks a lot.