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

Michael Rushton
Michael Rushton
7,126 Points

Really Stuck on this one. Getting an error when i add a different value for the unit

Right, this is what im doing....

function getArea(width = 45, length = 69, unit = 'square feet') { const area = width * length; return ${area} ${unit}; }

thats my code, and if i pass the "unit" parameter an argument that is a number it works, but not if i send it a text string. For instance if I call getArea(5,10,miles)

the console gives me this error:

VM131:1 Uncaught ReferenceError: miles is not defined at <anonymous>:1:13

but ONLY when i put in a text string instead of a number. It still works for the default input being 'square feet' if its left blank but surely i need to be able to put in an argument that is one made of words not numbers

In fact, now i think of it, i'm not even sure how you define what each parameter of a function's type will be??? Did i miss something? How do I tell the browser that "length" should expect a number for instance?

Even so, you'd think that if i set the "unit" parameter to have a default of a string, it would be able to accept another string???

I think im probably a bit tired but this is making no sense. HELP!!

2 Answers

Hi Michael,

if you pay close attention to the error message it actually gives you a really nice hint...

"miles is not defined..." means that miles is seen as a variable name and not as a string. So the javascript interpreter is looking for a variable declaration of miles but cannot find it.

If you want it to be a string parameter you have to wrap it in single or double quotes. With numbers this is exactly the opposite: you can't use quotes around them (as you correctly did). If you put quotes around numbers they will be interpreted as strings.

Does that make sense?

Let me know if it is not clear yet.

Blessings from Berlin and happy java-scripting, Nils

PS: If my answer helped you or solved your issues, please upvote my answer and/or mark it as "Best answer" (so people browsing the community forum know your issue is solved)

Michael Rushton
Michael Rushton
7,126 Points

Thanks so much Nils! That really helped me get over a hurdle. Greetings from the UK.

Mike

Great. Happy I could help. :-)