Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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,383 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,486 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,822 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..