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 Making Decisions in Your Code with Conditional Statements Boolean Values

My console keeps printing the else statement even when i guess the correct number.

var correctGuess = false; const number = 6; const guess = prompt('guess a number between 1 and 10');

if ( +guess === number ) { correctGuess === true; }

if ( correctGuess ) { console.log('you guess the number.'); } else { console.log(Sorry the number is : ${number}); }

1 Answer

Hi Kamarky, === is the strict equality operator, while = is the assignment operator.

In your code you never actually change the value of correctGuess to true. You simply check if correctGuess is true, which it isn’t because in the first line you set it to be false.

So in the final if...else statement the else always runs because correctGuess will always be false.

In the section of code where you’re attempting/meaning to change correctGuess to true, use the assignment operator instead of the strict equality operator.

The rest of your code is perfect. Nice job, coder.