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

Raghav Mangrola
Raghav Mangrola
3,050 Points

What'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
PLUS
adam åslund
Courses Plus Student 12,184 Points

Yes 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.

Hey 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
PLUS
Saira Bottemuller
Courses Plus Student 1,749 Points

Those are really great answers, thank you guys - this wasn't my question but I've still learned from you. Good question as well! :)