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 trialevans tsai
3,735 PointsI 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.
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>') }
2 Answers
Thomas Nilsen
14,957 PointsReplace
correctGuess === true;
with
correctGuess = true;
evans tsai
3,735 Pointsthank u!!!
Euston Francis
365 Pointsvar 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.
evans tsai
3,735 Pointsevans tsai
3,735 Pointsbtw, I don't know how to put the code image. ?