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 trialMartin Möhlmann
7,824 PointsJavaScript doesn't seem fit to parse the "decimals" of octimal numbers, i.e. the numbers after the point. Thoughts?
I would like to hear if there are ways around this problem, or whether it is in fact a limitation of the JS language.
To specify what is going on. Instead of parsing the string as an octimal number, the following code:
console.log(parseFloat("10.135", 8));
just returns 10.135. If I enter the following code:
console.log(010.135);
the return reads: "Uncaught SyntaxError: Unexpected number"
So, am I correct in concluding that JS cannot work with non-ten-based numbers in "decimals".
2 Answers
La Kenzi
14,346 PointsThe parseFloat() function parses a string and returns a floating point number.
This function determines if the first character in the specified string is a number. If it is, it parses the string until it reaches the end of the number, and returns the number as a number, not as a string.
Note: Only the first number in the string is returned!
Note: Leading and trailing spaces are allowed.
Note: If the first character cannot be converted to a number, parseFloat() returns NaN or in other words this error "Uncaught SyntaxError: Unexpected number".
console.log(010.135);
Uncaught SyntaxError: Unexpected number
console.log(parseFloat("010.135"));
10.135
Hopefully this gives a better understanding of how JavaScript interprets numbers and decimal numbers. http://www.w3schools.com/jsref/jsref_parsefloat.asp
La Kenzi
14,346 PointsYes, when using console.log(010.135); JavaScript can't work with non-ten-based numbers, it can only work if you use syntax from the above post.