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 Pass Information Into Functions Pass Multiple Arguments to a Function

karan Badhwar
seal-mask
.a{fill-rule:evenodd;}techdegree
karan Badhwar
Web Development Techdegree Student 15,454 Points

Predefined Parameter

function getArea(width, length, unit){ unit = "sq. ft." const area = width * length; return ${area} ${unit}; }

If I try to enter a new value for unit it won't, is that because of the fact that argument is receiving a value in the beginning and then the value is getting assigned ?

1 Answer

Trent Nelson
seal-mask
.a{fill-rule:evenodd;}techdegree
Trent Nelson
Full Stack JavaScript Techdegree Student 17,509 Points

Hey there, several problems exist that I can see.

  1. You're immediately assigning a value of "sq. ft." to the unit variable. If you try to provide your own value like "meters", it's immediately overwritten. To correctly assign a default value to "unit" you need to declare it in the parameter field of getArea().

  2. Your return statement will also error due to the unexpected "}" from your handlebar variables. To resolve this wrap your statement in the proper template literal syntax using the ` character.

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

getArea(10,20,"Meters");
//Should properly return "200 Meters".

Hope this helps!