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

Functions Challenge task 1 of 1

Can anybody supply the code for this challenge? I'm completely stuck. Thanks.

5 Answers

The Instructions state to create a function that takes in an input and returns 0 if it is either a string, a number or an undefined value. If the input is an array it should return it's length;

Step 1:

Create the function with a parameter. In this case I have chosen 'item'.

function arrayCounter(item){

  //Empty function

}

Step 2:

Check to see if the 'item' is an array or not. I will use the typeof operator in order to identify a string, a number or a an undefined value. if the value is an array I will simply return it's length using the length method otherwise I will simply return the number 0.

//Final Function

function arrayCounter(item){

        if(typeof item === "string" || typeof item === "number" ||typeof item ===  "undefined")
        {
          return 0; 
        }else
        {
          return item.length;
        }
  }

Hope this helped. Happy programming. Have a good day.

What are these signs | | that you're using? I haven't learned these yet.

||

Stands for "OR".

This is probably why I'm having trouble with this. I haven't even learned this yet. I really appreciate your help. Thank you.

No problem.

So in short, the program, in english would simply state:

If the item's "type" is a string OR(||) a number OR(||) an undefined value then return 0.

I really appreciate your help. Thank you man!