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 trialSusan Trachsel
5,489 PointsFunctions: Return Values Challenge
Challenge is: Around line 17, create a function named 'arrayCounter' that takes in a parameter which is an array. The function must return the length of an array passed in or 0 if a 'string', 'number' or 'undefined' value is passed in.
I have tried these but keep getting an SyntaxError - Parse Error. I'm confused by how to format the argument or if I was supposed to define an array variable first? I'm missing something obviously and I'm stuck here.
function arrayCounter (array){
if(typeof array === 'undefined', 'number', 'string') {
return = 0;
}
console.log(array.length);
}
and also this:
function arrayCounter (array){
if(typeof array === 'undefined', 'number', 'string') {
return = 0;
}
return =array.length;
}
1 Answer
Jason Anello
Courses Plus Student 94,610 PointsHi Susan,
You're pretty close. You have to check the type against each string separately rather than join them all with the comma operator. Also you want to return the length of the array at the end without the equal sign return array.length;
Same thing with return 0;
No equal sign.
function arrayCounter (array){
if(typeof array === 'undefined') {
return 0;
}
if(typeof array === 'string') {
return 0;
}
if(typeof array === 'number') {
return 0;
}
return array.length;
}
Each type is checked separately. There are better ways to solve this problem but that is about what can be expected of you at this point.
Susan Trachsel
5,489 PointsSusan Trachsel
5,489 PointsThat was the next thing I was wondering if we had to do several if statements. Dang, if only there was a shorter way.. :)
thank you, glad I was at least on the right track!
Jason Anello
Courses Plus Student 94,610 PointsJason Anello
Courses Plus Student 94,610 PointsWith the logical OR operator,
||
, you can combine the if statements and remove that duplication. I don't think this has been introduced yet.You can read that as "if the type is undefined OR the type is string OR the type is number then return 0"
Probably the best way is to use the
Array.isArray()
method.Jason Anello
Courses Plus Student 94,610 PointsJason Anello
Courses Plus Student 94,610 PointsYou're welcome.
Yes, that 1st solution is the long way but as you learn more you'll be able to do it in shorter and more robust ways.
Susan Trachsel
5,489 PointsSusan Trachsel
5,489 PointsThat all makes sense! Thank you for taking the time - I am just so glad CodeOregon is providing this opportunity. I'm really looking forward to using this hopefully in a new career someday....
Jason Anello
Courses Plus Student 94,610 PointsJason Anello
Courses Plus Student 94,610 PointsBest of luck to you!
eck
43,038 Pointseck
43,038 PointsThis is a slightly shorter, more direct way to condition the return value:
Jason Anello
Courses Plus Student 94,610 PointsJason Anello
Courses Plus Student 94,610 PointsHi Erik,
I could be wrong but I don't think you would know at this point that the
typeof
operator will return 'object' for an array. Also, while this is shorter than what I think is the expected solution it still suffers from similar problems.An
object
ornull
could be passed in and these will also make theif
condition true. Then you'll either be returningundefined
in the case of theobject
or generate a typerror withnull
becausenull
has no properties.The original solution I posted also suffers from these same types of problems. It does fail on more things than your solution though.
The
isArray
method is also short and doesn't suffer from these problems.eck
43,038 Pointseck
43,038 PointsThanks Jason, I didn't know until now that a value of null would pass that condition :P