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 trialMahfuzur Rahman
3,204 Pointsalways 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); }
3 Answers
Cory Harkins
16,500 PointsRemove 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
19,870 PointsThe string guess is never going to be identical to the number browser (===). Try toString()
on browser.
Patti Harmon
1,101 PointsI think it should be var browser= Math.floor( Math.random() * 6) + 1; Give it a try..
Matthew Long
28,407 PointsMatthew Long
28,407 PointsEither convert the string to an Int type, or simply use
==
instead of===
for your conditional statement.