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 trialja5on
10,338 PointsLast part of the video im stuck
Don't understand this video
function getArea(width, length, unit = "quantity" ) {
const area = width * length;
return ${area} ${unit} ${quantity}
;
}
Is this video really important, just seems to be using console all the time.
ja5on
10,338 Pointshe gave a short practice at the end of the video that i'm stuck on
7 Answers
Happy .
11,414 PointsAh okay i'm following now, it's just asking you to set unit to a default value to fallback on, so 'sqft' for example, which means unless told to change (by some interaction on the web page or customer input) it will always return 'sqft'. so if you call a function without giving it parameters it will still run
function getArea(width = 0, length = 0, unit = 'sqft') {
const area = width * length;
return `${area} ${unit}`;
}
call in console with getArea(5,4,'sqm') returns "20 sqm"
call in console with getArea(5,4,unit) returns "20 sqft"
call in console with getArea() returns "0 sqft"
the issue with what you've written is you've set the value of unit to "quantity" , which is fine, but you've then passed ${quantity} into your return, however quantity does not exist.
does that help further?
ja5on
10,338 PointsOh thanks
Happy .
11,414 Pointsyou'll have to forgive me it's been a while since I've done this:
function getArea(width = 45, length = 69, unit = 'square feet')
you're implying that the variables width & length are numbers because you're setting values of numbers, whereas you've said unit equals a string
getArea(5,10,miles)
should read:
getArea(5,10,'miles')
because the last parameter is expecting a STRING of text.
calling
getArea(5,10,unit)
is calling the variable UNIT by name, but you've set the initial value to 'square feet', so when you call it, it returns 'square feet'
does that help explain ? :)
Michael Rushton
7,126 PointsAhhhhh thanks so much!! Its because i was'nt feeding it a string in the first place!! Thanks so much for your help, what a relief.
Mike
Happy .
11,414 PointsSometimes it's the most obvious things that confuse us! Using meaningful parameter/variable names will help mitigate some of the confusion.
Suggestions for this example could be: getArea(areaWidth = 45, areaLength = 69, defaultUnit = 'square feet')
I'm glad this was of some help, it's reassuring to know I've learned at least something :D
Michael Rushton
7,126 PointsSeriously got me out of a hole there! Your answer was really concise and accurate too. Hope you have an amazing day.
Mike
wildgoosestudent
11,274 PointsI went about this a different way! In the video he was talking about making the units country specific so I put country as one of my parameters.
function getArea(width, length, country) {
if (country.toUpperCase = 'US') {
unit = 'sq ft'
} else {
unit = 'sq m'
}
const area = width * length;
return ${area} ${unit}
;
}
Happy .
11,414 Points@wildgoosestudent that's great, you could even kick it up another notch with swapping the if statement for a switch statement for multiple countries or continents!
Michael Rushton
7,126 PointsIm stuck on this too badly.
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
Michael Rushton
7,126 PointsIn 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!!
Emon Ahmed
2,645 Pointsfunction getArea (width=0,length=0,unit="sqf"){
const area = width*length;
return ${area} ${unit}
}
console.log(getArea(45,69)) // It should give you 45*69 (sqf default) bcoz in the parameter you set the default value as sqf . if you now you want change the value in mile perhaps, then
console.log (getArea(45,69,"miles")) //you should write the unit in " " or ' '
Happy .
11,414 PointsHappy .
11,414 PointsWhat are you struggling to get your head around?
Arrow functions, aren't a necessity, but they do save extra lines of coding. and the reason you're using the console to log returns is so you can see how things work, before trying to code something more complex.
Additionally your code isn't what you're shown in the video,
character to the left of the 1 key, not to be confused with single quotes ' ' or double quotes " " on your return .
I'm assuming you mean it to read:
try not to deviate from the instructions if you don't fully follow it, the more you do the more it will all start to click