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 Improving the Random Number Guessing Game

James Head
James Head
18,872 Points

Use of parseInt()

In the code on this video we use parseInt every time we test a condition. I remember Dave saying we shouldn't repeat code if it is not necessary. Would it not make more sense to convert the string to a number at the time the user inputs it? So.. use parseInt(prompt('Enter your number'));

Darren Healy
seal-mask
.a{fill-rule:evenodd;}techdegree
Darren Healy
Front End Web Development Techdegree Student 23,565 Points

Hi James,

While D.R.Y. is something we should all stick to, I think it is just there as clarification for beginners. I wouldn't read into it too much.

Darren

4 Answers

Dave McFarland
STAFF
Dave McFarland
Treehouse Teacher

Hi James Head,

You've got a very good point and Darren Healy is correct about why I used parseInt() so many times. Your solution -- parseInt(prompt('Enter your number')) -- is very good, but I didn't use that because that solution involves a lot of mental steps for someone new to the language who might be confused to see so many JavaScript methods combined in a single statement like that.

While "don't repeat yourself" is very good programming, "do repeat yourself" is a good way to learn!

I'm glad you could put the course ideas to good use and figured out a BETTER way to write the code!

@james barnett's solution is also good: store the result of the parseInt() method in a variable and use that variable instead of repeating the use of the parseInt() method.

James Barnett
James Barnett
39,199 Points

Personally I think this is more readable / maintainable

var rawNum = prompt('Enter your number');
var num = parseInt(rawNum);

than this

var num = parseInt(prompt('Enter your number'));
James Head
James Head
18,872 Points

Thanks James, I agree this looks much more readable.

Don't forget the radix parameter.

var num = parseInt(rawNum, 10);

Per MDN doc - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt:

radix

An integer between 2 and 36 that represents the radix (the base in mathematical numeral systems) of the above >mentioned string. Specify 10 for the decimal numeral system commonly used by humans. Always specify this parameter to eliminate reader confusion and to guarantee predictable behavior. Different implementations produce different results when a radix is not specified.

James Head
James Head
18,872 Points

Thanks everyone for your replies.