Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript Functions Arrow Functions Default Function Parameters

Tural Rzazade
Tural Rzazade
3,397 Points

Solved the task in the last part

const regionDown = prompt("Where are you from, Europe or America?");
const region = regionDown.toUpperCase(); 

if (region === "AMERICA") {
    function getArea(width, length, unit = "sq ft") {
      const area = width * length;
      return `${area} ${unit}`;
    }
  } else {
    function getArea(width, length, unit = "sq ft") {
      const area = width * length;
      return `${area} ${unit}`;  
    }
  }

I struggled first to figure out the way for getting unit based on the region that user is from, but then I used a conditional statement. I knew that I had to use conditional smth to get that but I guess "little things" (indentation/ quotes/ misspelled words) were making me think that it doesn't work.

1 Answer

Steven Parker
Steven Parker
218,692 Points

It's not clear why you would use a conditional here, since both branches contain exactly the same code. Did you perhaps intend for the 2nd definition to set the default unit to "sq meters" instead?

Also, instead of having two completely different definitions of the function, you could have just one and make use of the region setting inside the function code, or in the way the function is called.