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

evans tsai
evans tsai
3,735 Points

I can't find problem in my code.

When I entering the right answer, it still shows "sorry the number is ..." . It never shows "Cool! that is right answer. " I can't find where go wrong. here is my code.

http://imgur.com/a/Atblo

var guess = prompt("guess a number from 1 to 6"); var number = Math.floor(Math.random()*6)+1; var correctGuess = false;

if (parseInt(guess) === number){ correctGuess===true; } else if(parseInt(guess)<number){ var guessMore = prompt(" I think the number is bigger than "+ guess+"."); if(parseInt(guessMore) === number){ correctGuess === true; } } else if(parseInt(guess) > number){ var guessLess = prompt("I think the number is less than" + guess + '.'); if (parseInt(guessLess) === number){ correctGuess === true; } } if (correctGuess){ document.write('<p> cool! that is right answer. </p>') } else { document.write('<p> sorry the number is '+ number +' .</p>') }

evans tsai
evans tsai
3,735 Points

btw, I don't know how to put the code image. ?

2 Answers

Thomas Nilsen
Thomas Nilsen
14,957 Points

Replace

correctGuess === true; 

with

correctGuess = true; 

var guess = prompt("guess a number from 1 to 6"); var number = Math.floor(Math.random()*6)+1; var getAnswer = "answer" function correctGuess(){ if (parseInt(guess) === number){ answer=true; } else if(parseInt(guess)<number) { var guessMore = prompt(" I think the number is bigger than "+ guess+" .")}; if(parseInt(guessMore) === number){ answer = true; } else if(parseInt(guess) > number){ var guessLess = prompt("I think the number is less than" + guess + ' .'); if (parseInt(guessLess) === number){ answer =true; } } document.write('<p> cool! that is right answer. </p>') } else { document.write('<p> sorry the number is '+ number +' .</p>') }

You have already assigned a false value to correctGuess therefore it can't be true at the same time. This might work.