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 Boolean Values

jbiscornet
jbiscornet
22,547 Points

Something missing? Why is it bypassing the if (true) statement and giving me the else (false) statement every time?

Can someone help me figure this out? I followed exactly as he said (or so I think) in the video and every time I run the code it comes up with the false statement, even if I guess the number right. Any help is appreciated, Thanks.

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; } if (correctguess === true) { document.write('<p> You guessed right!</p>'); } else { document.write("<p> Sorry that's not correct. The number was " + randomNumber + ".</P>"); }

1 Answer

andren
andren
28,558 Points

You have a typo in this code:

if (parseInt(guess) === randomNumber) {
    correctguess === true; // You are comparing correctguess to true, not setting it
}

By using === you ask JavaScript to compare the values, not set it like you are meant to. If you change it to = like this:

if (parseInt(guess) === randomNumber) {
    correctguess = true; // Set correctguess equal to true
}

Then your code will work.

jbiscornet
jbiscornet
22,547 Points

That works, thanks for the correction.

Yemaya Re
Yemaya Re
Courses Plus Student 1,922 Points

Thanks!! It was driving me crazy wondering what's wrong.