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 trialSamantha Atkinson
Front End Web Development Techdegree Graduate 40,307 PointsWhy 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
231,275 PointsSince 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
Front End Web Development Techdegree Graduate 40,307 PointsSamantha Atkinson
Front End Web Development Techdegree Graduate 40,307 PointsHi 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
231,275 PointsSteven Parker
231,275 PointsI confused myself for a moment! But it's the conversion process that's important:
Samantha Atkinson
Front End Web Development Techdegree Graduate 40,307 PointsSamantha Atkinson
Front End Web Development Techdegree Graduate 40,307 Points🤣 thank you, Steven, you may have confused yourself, but you gave me clarity!