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 trialOggie Prima Dita Putra
7,695 PointsIncrease difficulty
Hi,
I successful try random guessing number as video says. But i want to improve the guessing number by adding extra info, (info that your second guess is still MORE/LESS than randomNumber), this is my code, but when i try the second info is not running, what happens to my code?
var correctGuess = false;
var randomNumber = Math.floor(Math.random() * 10 ) + 1;
var guess = prompt('I am thinking of a number between 1 and 10. What is it?');
if (parseInt(guess) === randomNumber ) {
correctGuess = true;
} else if (parseInt(guess) < randomNumber){
var guessMore = prompt('The number is more than ' + guess);
if (parseInt(guessMore) === randomNumber) {
correctGuess = true;
}
} else if (parseInt(guessMore) < randomNumber){
var guessMoreMore = prompt('The number is still more than ' + guessMore);
if (parseInt(guessMoreMore) === randomNumber) {
correctGuess = true;
}
} else if (parseInt(guessMore) > randomNumber) {
var guessMoreLess = prompt('The number is now greater than ' + guessMore);
if (parseInt(guessMoreLess) === randomNumber) {
correctGuess = true;
}
}
else if (parseInt(guess) > randomNumber){
var guessLess = prompt('The number is less than ' + guess);
if(parseInt(guessLess) === randomNumber) {
correctGuess = true;
}
} else if (parseInt(guessLess) > randomNumber) {
var guessLessLess = prompt('The number is still less than ' + guessLess);
if (parseInt(guessLessLess) === randomNumber) {
correctGuess = true;
}
} else if (parseInt(guessLess) < randomNumber) {
var guessLessMore = prompt('The number is now greater than ' + guessLess);
if (parseInt(guessLessMore) == randomNumber) {
correctGuess = true;
}
}
if ( correctGuess ) {
document.write('You guessed the number! ' + randomNumber);
} else {
document.write('Sorry the number was ' + randomNumber);
}
```
2 Answers
J Donahue
30,790 PointsSo I messed around with your code a bunch. At first I was trying to straighten out the if/else if/else blocks to try and retain your original concept. However, it was crossing my eyes, and I knew there was a simpler way to execute this. I don't explicitly say whether the users guess is "still" less than or greater than. I merely indicate that it is. That saves one from having to keep creating new variables to hold the users current guess. Here is my solution:
function guessNumber() {
var correctGuess = false;
var randomNumber = Math.floor(Math.random() * 10) + 1;
var guess = prompt('I am thinking of a number between 1 and 10. What is it?');
//Here we can simply use a for loop to continually reassign the variable guess
// rather than to continually create new variables to hold the users current guess
for (var i = 0; i < 3; i++) {
// This code will break you out of the loop if you press cancel at the prompt
if (guess === null) {
break;
}
if (guess < randomNumber) {
guess = prompt("The number is greater than " + guess);
} else if (guess > randomNumber) {
guess = prompt("The number is less than " + guess);
} else {
correctGuess = true;
}
}
if (correctGuess === true) {
document.write("That's Correct!");
} else {
document.write("Sorry, better luck next time.");
}
}
guessNumber();
Here it is on JSFiddle: https://jsfiddle.net/JeremyPDonahue/ocfkrjhz/ However, on JSFiddle I am using alerts rather than document.write because that particular method isn't allowed on the site. It breaks the page when attempting to save.
Oggie Prima Dita Putra
7,695 PointsThanks Jeremy, after reading your answer, I think that i ahead of my knowledge of JS. I'll keep learning. Thx very much
J Donahue
30,790 PointsYou're welcome. If you have a specific question about it, I'd be happy to explain it further.
Kevin JULLIEN
985 PointsKevin JULLIEN
985 Pointspoor dude just learned what an if statement is and you respond to his question with a loop. topkek