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

Up to two decimal places

https://w.trhou.se/y3jj1i68cj

Hi, in my code, one answer is 8.049999999999999 SqM I know I could reduce to 8.05 and that would be great because say it was a program for working out measurements. I have seen toFixed(2) but I'm at a loss as to where to insert that, or is there a different method to use?

Any help, thanks.

3 Answers

OK, I see now, you need these `` around the template literal:

function getArea(width, height, unit) {
  var area = width * height;
  var a = area.toFixed(2);
  return `${a} ${unit}`;
}

console.log(getArea(10.2, 20.495, 'sq ft'));

Output: 209.05 sq ft

Yes I thought earlier that maybe on line var area = width * height; that maybe I could "get away" with adding .toFixed(2); I'm not against more lines of code, but I can see that line var area = width * height; has to declare the variable first.

Well, you could do these:

function getArea(width, height, unit) {
  return `${(width * height).toFixed(2)} ${unit}`;
}

But I spaced it out early to try and explain your issue.

Try this:

var num = 8.049999999999999;
var n = num.toFixed(2);

That doesnt work with my code function getArea(width, height, unit) { var area = width * height; var a = area.toFixed(2); return ${area} ${unit}; }

Not sure what your doing with the return part, but try this simple example first:

function getArea(width, height, unit) {
  var area = width * height;
  var a = area.toFixed(2);
  return a;
}

console.log(getArea(10.2, 20.495, 10));

Output was 209.05

return ${area} ${unit}; Is a template literal. Without them I cannot access units.