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

JavaScript JavaScript Basics (Retired) Making Decisions with Conditional Statements Improving the Random Number Guessing Game

R R
R R
1,570 Points

This code seems to work. Is it correct? It doesn't match the solution in the video.

This was written before i watched the solution. Is there anything wrong with this code? If not, is there an advantage to doing it the way shown in the video?

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) {
  correctGuess = false;        
  var guess = prompt("That number is lower than the answer. Try again.");

} else if (parseInt(guess) > randomNumber) {

  correctGuess = false;
  var guess = prompt("That number is greater than the answer. Try again.");
}
  else (correctGuess = true);

if ( correctGuess ) {
    document.write('<p>You guessed the number!</p>');
} else {
    document.write('<p>Sorry. The number was ' + randomNumber + '.</p>');
}
R R
R R
1,570 Points

not sure why the type looks odd at the end of the code. And don't know why I can;t just display my code in a window like usual. Apologies.

Dave McFarland
Dave McFarland
Treehouse Teacher

Hi,

To put code into a forum post use triple back ticks -- ``` — around the code. I fixed your code here, but in the future here's a forum discussion that describes how to add HTML, CSS, JavaScript or other code to the forum: https://teamtreehouse.com/forum/posting-code-to-the-forum

5 Answers

Dave McFarland
STAFF
Dave McFarland
Treehouse Teacher

Hi,

You need to nest if statements for this to work. What you currently have is a series of if, else/if statements that work like this:

  1. is answer correct? yes/no
  2. if 1 is no, then is answer < correct answer? yes/no. If yes, then open prompt saying "That number is lower".
  3. if 2 is no, then s answer > correct answer? yes/no. If yes, then open prompt saying "That number is higher".

The problem is that you don't test what the user inputs in steps 2 and 3 to see if they provided a correct answer.

In other words, if the answer is wrong, then you need to add more if / else statements to check whether the user is above or below the number, then ask again, then check again (another conditional statement).

Ehsan Asgari
Ehsan Asgari
6,151 Points
var correctGuess = false;
var randomNumber = Math.floor(Math.random() * 6) + 1;
console.log(randomNumber);
var guess = prompt('I am thinking of a number between 1 and 6, what is it?');

if ( randomNumber === parseInt(guess) ) {
  correctGuess = true;
}
  else if ( randomNumber < parseInt(guess)  )  {
    guess =  prompt('Sorry, wrong guess. Try again: (hint: your last guess (' + guess +') was too big.)');


  } 

 else if ( randomNumber > parseInt(guess) ) {
    guess =  prompt('Sorry, wrong guess. Try again: (hint: your last guess (' + guess +') was too small.)');

  } 

if ( randomNumber === parseInt(guess) ) {
  correctGuess = true;
    }

if ( correctGuess ) {
  document.write('<p> Correct </p>');
}  else {
  document.write('<p> Sorry wrong. The correct number was ' + randomNumber + ' .</p>');
}

You could skip the individual testing and just give it another go at the end of the tests.

R R
R R
1,570 Points

Thanks, Dave. Enjoying your vids very much, btw. :-)

R R
R R
1,570 Points

Thank you, Dave. That makes perfect sense.

with else you don't put parentheses