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 trialRaghav Mangrola
3,050 PointsWhat's the best practice for this?
Is it ok to write it as
var guess = parseInt(prompt('I am thinking of a number between 1 and 6. What is it?'))
instead of
if (parseInt(guess) === randomNumber) {
3 Answers
adam åslund
Courses Plus Student 12,184 PointsYes that's fine. As long as you are only expecting the user to put in numbers. If there are multiple questions asked that mixes numbers with strings then you would want it to parseInt in the if statement.
Marcus Parsons
15,719 PointsHey Raghav Mangrola,
It really depends on what you're trying to do with the variable guess
. If you were going to manipulate the guess variable after you prompted for a value, the optimal solution would be to go ahead and parse the integer at initialization (the 1st way). But, if you only needed to get the integer out one time, you could just do it in the if-statement. If I had to choose a best method, I would say the first method, because you could do other number manipulation after it was initialized unlike with the parsing it during the if-statement. Stick with the 1st method is my suggestion.
Saira Bottemuller
Courses Plus Student 1,749 PointsThose are really great answers, thank you guys - this wasn't my question but I've still learned from you. Good question as well! :)