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
elpsycongroo
2,368 PointsWhy does this still work without parseInt?
var randomNumber = getRandomNumber(10);
var guess;
var guessCount = 0;
function getRandomNumber( upper ) {
var num = Math.floor(Math.random() * upper) + 1;
return num;
}
do {
guess = prompt("Pick a number from 1-10");
guessCount += 1;
} while(guess != randomNumber);
document.write("<h1>You guessed the number!</h1>");
document.write("It took you " + guessCount + " tries to guess the number");
Shouldn't it have to be while(parseInt(guess) != randomNumber); considering prompt converts the users input to a string?
Thanks!
1 Answer
Christopher Augg
21,223 PointsHeya,
It is due to how js uses comparison operators.
3 == "3" is true - this does not test for type comparison (int equals String)
3 === "3" is false - this does test for type
Same goes for != and !==
Check out [W3Schools]https://www.w3schools.com/js/js_comparisons.asp)