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 JavaScript Foundations Functions Return Values

Neslee Rodillo
Neslee Rodillo
19,615 Points

where am i going wrong

task: 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.

My solution:

function arrayCounter (number, string){
        if (typeof string === 'undefined'){
                return string == 0;
        }
         if (typeof number === 'undefined'){
            return number ===0;
        }  

      };

3 Answers

Andrew McCormick
Andrew McCormick
17,730 Points

Neslee, Good try, but you have several things going on here:

  • first you have two arguments being passed into the function. where you should just have one that you plan to be an array.
  • second in your return statement you are using comparison equality operators which would only return a boolean. ( return foo === 0 vs return 0
  • you should be testing the one argument you pass in 3x. First to see if it's a string, then a number, and finally to see if it's undefined.
  • finally the question asks if the argument is an array then return it's length. no where are you attempting to do that.

you might want to rewatch the videos for this section. Of course if you just want the answer you can search the forum and find this: the answer

Neslee Rodillo
Neslee Rodillo
19,615 Points

Thank you so much, I studied Java when at uni but did not understand it, now learning Javascript and still don't understand but your explanation really helped.

T眉nde Mat贸cza
T眉nde Mat贸cza
11,194 Points

I'm just giving a clue: The parameter of the function arrayCounter should be an array, not (number, string).

Neslee Rodillo
Neslee Rodillo
19,615 Points

is this what you mean (i am soo completely clueless with Javascript):

'''javascript function arrayCounter (array){ if (typeof array = 'undefined'){ return 0; } };'''