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 trial1 Answer
jb30
44,806 PointsparseFloat
tries to parse the string until it reaches an invalid value, such as "S", then returns the float it has parsed before the invalid value.
If you are able to modify the getArea
function, you could round the area before returning the string. If you need the unrounded area, you could return the array [area, units]
, then print out the answer string using
let myCalc = getArea(10.5, 28.2, "Sq ft");
console.log(`${myCalc[0].toFixed(2)} ${myCalc[1]}`);
If you want to avoid modifying the getArea
function, you could use the split
function and do
myCalc = myCalc.split(" ")
myCalc[0] = parseFloat(myCalc[0]).toFixed(2);
console.log(myCalc.join(" "));
You could also use regular expressions to get the number part of myCalc
.
ja5on
10,338 Pointsja5on
10,338 PointsI got the first method you suggested to work thank you very much.