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

Jacob Miranda
Jacob Miranda
19,080 Points

JS Foundation - Return Values

I've tried everything! If anyone can explain this to me, I'm not understanding too well what it's asking for

The challenge says:

"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."

Edit: I can't get the Anonymous Function code challenge to work either -.-

anthony de alwis
anthony de alwis
4,146 Points

here is the answer dude:

function arrayCounter(array){

        if(typeof array==='string'){
        return;
        }
        if(typeof array==='number'){
        return;
        }
        if(typeof array==='undefined'){
        return;
        }
    else{ return array.length;}
       };

6 Answers

Jacob, I cannot figure this one out either. However, I was able to figure out the anonymous functions challenge.

part 1:

place the anonymous function after the 'anonymousFunction' variable

part 2:

place the () after the 2nd function to execute it

Stone Preston
Stone Preston
42,016 Points

Here is a psuedocode version of the function that should help

function arrayCounter ( array) 

    if (array is of type 'array')

        return length of array

    else

        return 0
anthony de alwis
anthony de alwis
4,146 Points
function arrayCounter(array){

        if(typeof array==='string'){
        return 0;
        }
        if(typeof array==='number'){
        return 0;
        }
        if(typeof array==='undefined'){
        return 0;
        }
    else{ return array.length;}
       };
Jacob Miranda
Jacob Miranda
19,080 Points

Why is what I put wrong?

function arrayCounter(array) {
    }
    if (typeof array === 'string') {
        return 0;
    }
    if (typeof array = 'number') {
        return 0;
    }
    if (typeof array = 'undefined') {
      return 0;
    }
  else { return array.length; }
  }
Jacob Miranda
Jacob Miranda
19,080 Points

actually that's not what I put. Woops, wrong one. But I got it to work. Thanks guys.

Andrew Chappell
Andrew Chappell
12,782 Points

So you have to create seperate if statements for 'number', 'string' and 'undefined' and set them all to return 0; instead of just return;

Here is the code:

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

return array.length;
}
anthony de alwis
anthony de alwis
4,146 Points

don t forget else { return array.length;}

Chase Lee
Chase Lee
29,275 Points

Here is a thread on how to display code in the forum.

Jacob Miranda
Jacob Miranda
19,080 Points

Oh I know how to, I just didn't put my code cause I was way off.

John Shofstall
John Shofstall
14,308 Points
function arrayCounter(array) {
    if (type of array === 'string' | 'number' | 'undefined') {
      return 0;
    }
    return array.length;
  }

why is that wrong? i can't simplify into one if statement with ors? "|" is the character for "or" right?

John, the or operator is || not |.
Hope this helps.

John Shofstall
John Shofstall
14,308 Points

thank you, that will prevent future errors. but that still didn't work, i guess we have to write separate statements for now.