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.

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>');
}
```

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.

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

STAFF

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:

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).

```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.

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

Thank you, Dave. That makes perfect sense.

with else you don't put parentheses