Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript

Function Challenge Help

Can anyone point me in the right direction with this challenge

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.

Bummer! Wrong value returned from 'arrayCounter(undefined)' it should be 0

      function arrayCounter (my_array) {

        var my_array = [1,2,3];
        if (my_array === 'undefined')
          return 0;
        arrayCounter.length;
      }

5 Answers

Art Skupienski
Art Skupienski
7,080 Points

Hi Tyler,

I see your point to simplify the code which is in most cases good way. However there are some syntax rules we need to follow. Whenever you pass conditions to your if statement you need to be more specific. Otherwise your code may not be executed.

Add typeof array === before 'string' and also before 'number' and you will be ready to go :-) Keep going and have fun with Javascript!

Just like below:

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

Thanks Art! I was not aware if statements needed such specificity.

Art Skupienski
Art Skupienski
7,080 Points

You can try to rewrite this code using shorter way too.

variable = (condition) ? true-value : false-value;

However keep in mind when you simplify your code automatically it becomes less readable especially when is much more complex and it's for plugin development.

Art Skupienski
Art Skupienski
7,080 Points

Hi Steve,

In order to accomplish this you will need yo create function called 'arrayCounter' like this:

function arrayCounter(){
}

Now you need to pass a parameter which will be an array, however you do not need to create variable here. It can be any parameter name which you will then use inside this function. Let's say you array will be 'a':

function arrayCounter(a){

}

Now inside this function you have to use if statement to check if any argument you pass to function is a string or namer, or undefined. In order to defined type of your a parameter you passed in you use 'typeof':

      function arrayCounter(a){
        if(typeof a === 'string' || typeof a === 'number' || typeof a === 'undefined'){
          return 0;
        } 
          return a.length;          
      }

Best of luck with your Javascript course!

Art

Hi Art,

Why does this similar way not work?:

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

Thanks!

Tyler,

Try

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

The way you have it written is only checking if array === 'undefined', for it to work without the parenthesis you would need

(typeof array === 'undefined' || typeof array === 'string' || typeof array === 'number')

Thanks for the info Art!

Thanks for all the replies!

Steve