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 Build a Random Number Guessing Game

Mahfuzur Rahman
Mahfuzur Rahman
3,204 Points

always shows the else conditions. even when guessing is correct!

var guess=prompt('I am guessing a number between 1 and 6'); alert(guess); var browser= (Math.floor(Math.random()*6 +1));

if (guess===browser) {

alert('you guessed the number'+guess+', which was right');}

else {alert('you guessed the number '+guess+ ' , but it was'+ browser); }

Matthew Long
Matthew Long
28,407 Points

Either convert the string to an Int type, or simply use == instead of === for your conditional statement.

3 Answers

Cory Harkins
Cory Harkins
16,500 Points

Remove the '===' equals, and just stick with '=='.

The reason for this, is the input you are receiving from the prompt returns a string, not an integer.

'3' === 3 is false '3' == 3 is true

Although they are the same numeric value, their Type is different

String === Int false String == Int (maybe true)

Dave StSomeWhere
Dave StSomeWhere
19,870 Points

The string guess is never going to be identical to the number browser (===). Try toString() on browser.

I think it should be var browser= Math.floor( Math.random() * 6) + 1; Give it a try..