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 Introducing the Practice

Samantha Atkinson
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Samantha Atkinson
Front End Web Development Techdegree Graduate 40,307 Points

Why do I have to convert the user's number to a floating-point number?

Am I overthinking this? It seems to me I'm supposed to create some maths with 2 variables with integer numbers right? So why does it say in the comments:

//4. Convert that value from a string to a floating-point number

Shouldn't that be an integer number? Is it because if the user enters an integer number parseFloat returns an integer number anyway and if they do input a decimal number pareseFloat will do its thing?

1 Answer

Steven Parker
Steven Parker
231,261 Points

Since the numbers will be used to do math, the program should accept anything entered.

And it sounds like you nearly figured it out yourself, as you pointed out, a "parseFloat" will work with any number. But a "parseInt" would not correctly handle a non-integer, so it makes sense to convert the input as a floating-point number.

Samantha Atkinson
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Samantha Atkinson
Front End Web Development Techdegree Graduate 40,307 Points

Hi Steven, thank you for your reply. You've been kind enough to reply to previous questions of mine, thank you!

I'm sorry your last confused me.

"And "parseFloat" always returns a floating-point number, even if the input has no explicit decimal point."

I think why I'm confused is because I did a console.log and expected to see my integer number as 0.5 when I had input 5. So are you saying even an integer number is still considered a floating-point number by parseFloat regardless?

Steven Parker
Steven Parker
231,261 Points

I confused myself for a moment! :see_no_evil: But it's the conversion process that's important:

console.log(parseFloat("5.2"));   // outputs: 5.2
console.log(parseInt("5.2"));     // outputs: 5