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 trialCarla Thomas
Front End Web Development Techdegree Student 16,039 PointsReturn Values (Challenge)
I have been unsuccessfully working on this challenge for about 2 hours now. Please help.
Here are the instructions and what I have come up with.
Instructions
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
This is how I have broken this task down
// Create a function named arrayCounter with parameter (argument) 'array'
function arrayCounter (array) {}
// Function must return the length of an array passed in or 0
return array.length;
My only guess is that the "typeof array" is incorrect. Here is what I am submitting
function arrayCounter (array) {
if(typeof array === 'undefined'){
array = 0;
}
return array.length;
}
2 Answers
Jason Anello
Courses Plus Student 94,610 PointsHi Carla,
Your attempt is close and you have the right idea. You're checking if the value passed in is 'undefined' and that part is good except that you want to return 0 not set the array to zero.
Then you need 2 more if
blocks just like that one to check if either a 'string' or a 'number' was passed in. So you'll want to have 3 if
blocks and then all return 0.
At the end if you've made it past all those then you can return the length of the array.
Michelle Cannito
8,992 PointsYou can combine the 3 if blocks into 1 by using || which means "OR"
Jason Anello
Courses Plus Student 94,610 PointsThis would be the better way to do it and would remove the repetition but I don't believe it was covered at this point in the course.
You can do this with the logical OR operator:
if(typeof array === 'undefined' || typeof array === 'string' || typeof array === 'number'){
return 0;
}
This makes it more compact and removes the repetition.
Carla Thomas
Front End Web Development Techdegree Student 16,039 PointsCarla Thomas
Front End Web Development Techdegree Student 16,039 PointsThis answer did not work for me. What am I doing wrong? Not fully understanding the "0" part in the instructions.
Jason Anello
Courses Plus Student 94,610 PointsJason Anello
Courses Plus Student 94,610 PointsEdit: Oops I did array = 0; instead of return 0
You need 3 separate
if
blocks.Carla Thomas
Front End Web Development Techdegree Student 16,039 PointsCarla Thomas
Front End Web Development Techdegree Student 16,039 PointsThank you!
Jason Anello
Courses Plus Student 94,610 PointsJason Anello
Courses Plus Student 94,610 PointsYou're welcome. I forgot to answer "Not fully understanding the "0" part in the instructions"
The idea is that you want to return zero if any of those 3 types were passed in. This way you could test the return value of the function and if you see that it was zero then you know you must have passed in one of those 3 types, 'undefined', 'string', or 'number' and not an array.
Of course, you could pass in an empty array too and the function is also going to return zero.