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 trial
Doegena Fennich
8,974 Points'else' keeps popping up even if the answer is correct.
dont know where i went wrong.
here's my code;
var randomNumber = Math.floor(Math.random() * 9 ) + 1;
var question5 = prompt('I am thinking of a number between 1 and 10, i think of ...')
if ( parseInt(question5) === randomNumber) {
alert("Correct answer!")
score += 20;
} else if ( parseInt(question5) > randomNumber ) {
var questionLess = prompt('I think the answer is less than ' + question5 + ' [ One try left ]')
} if (parseInt(questionLess) === randomNumber) {
alert('Correct answer!')
score +=20;
}
else if (parseInt(question5) < randomNumber ) {
var questionMore = prompt('I think the answer is more than ' + question5 + ' [ One try left ]')
} if (parseInt(questionMore) === randomNumber) {
alert('Correct answer!');
score += 20;
} else {
alert('sorry, the answer was ' + randomNumber);
}
2 Answers
Korey Smith
11,804 PointsI ran the code and everything appears to be working properly. However I did notice that you wanted someone to guess a number between 1 and 10, but "var randomNumber = Math.floor(Math.random() * 9 ) + 1;" will only produce numbers 1-9. You would need to insert a 10 instead of a 9.
Tomasz Halaczkiewicz
21,845 PointsIt runs correctly but you're missing a couple things here. First of all you forgot to declare score variable, so even if you guess right there's nothing to add points to. Also you're missing a few semicolons, Chrome ignores it though.
Doegena Fennich
8,974 PointsThis is just a part of the code, as you can see this is the 5th question of my little quessing game. but thanks for the info about chrome.