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

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.

It keeps telling me that I'm missing a function names 'arrayCounter', what am I doing wrong? function arrayCounter(array) { if typeof array === 'undefined') { return 0; } return array.length; }

You have a syntax error. Look at your if statement

29 Answers

I needed to use "else if" & "else" and separate those values

There is an easier way to do this. What they were expecting, I believe, is to use the || statement to check with "or". You can, for bonus credit, just check if the parameter is an instance of an array. If it is, return the length. Else, return 0

Much easier :

function arrayCounter (a) { if (typeof(a) !== 'object') { return 0; } else { return a.length; } }

I worked this out to be:

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

This one worked for me, thanks.

What does | | do?

Thanks

Hi Alex,

The || sign means 'OR'. This way you don't have to rewrite code. It is called a logical operator.

More info can be found http://www.w3schools.com/js/js_comparisons.asp

Very confused, still. When did we learn the use of ||? Why would we learn typeOf if what we really need here is instanceOf? Else was not mentioned in this lesson, either.

Jim Hoskins may be worth updating this video, or changing the challenge a bit!

This code passed the challenge.

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

I don't get it.

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

why doesn't this work either? do we have to check typeof for each?

This is kind of a difficult lesson to follow along with.

Yea where did || come from? Can any body show us how this is done using what we've been taught up until this point instead of using instanceOf or ||

|| means 'or'

So

if(something happens || something else happens || something else else happens) {};

I went with the instanceof operator to see if the argument was an array. Took me at least 20 minutes of googling to figure out a way to get this done. @Zen, you're totally right, feels AMAZING to figure it out by yourself.

function arrayCounter(array) {
     if (array instanceof Array === true)
     {
     return array.length;
     }
     else
     {
     return 0;
     }
}

Can someone better define to me what instanceof does?

What challenge are you on?

Stage 5 functions Challege 1 of 1, ah I added the bracket and progress; now it says "wrong value returned from 'arrayCounter'("string")' it should be 0.

You are testing for undefined. Check what it asks you to look for.

Ah ok, more progress, now I've got function arrayCounter(array) { if (typeof array === 'string', 'number', 'undefined') { return 0; } return array.length; }

Bummer! If I pass in an array in to 'arrayCounter' of '1,2,3' I get 0 and not the number 3.

Look at your if statement. What exactly are you comparing? Hint: JavaScript isn't THAT intuitive and efficient.

I'm lost, when I type "1, 2 ,3" it into the console I get a return value of 3?

Ok I got it! Thanks heaps pal, you're a legend.

Can someone please help me with this as well? What I have so far:

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

Tiffany, I was just stuck on this as well.

Make sure you double check what your parameter ought to be called.

Also, take a look at Mozilla's developer tools (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/typeof) and see if this helps.

They are looking for the function to be able to return arrays only (or, almost only). So, it may be easier to think about this in terms of negation rather than equality (think !== rather than ===).

Sean Templeton's question "What exactly are you comparing?" is what helped me with this, although I'm not sure I went the way he was leading.

Hope this helps!

Ah! I got it! Thank you so much for your help :)

Rad! You're welcome.

Ok I have no idea what I am doing wrong. I'm getting the message "You're not checking if 'undefined' is being passed in"

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

I think this would have worked if you added typeof !== for every or condition

If I change to this

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

I get this message: If I pass in an array in to 'arrayCounter' of '1,2,3' I get 0 and not the number 3.

Never mind, I figured it out. Split up the if statement into three statements and don't use else between them, they aren't actually contingent on each other.

Doesn't it feel so gnarly when you figure it out yourself? Good job!

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.

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

Spent forever trying to use typeof for array, but an array is an object, so the function never worked right. Switched to using instanceof and it worked...

function arrayCounter (x) {
    if (x instanceof Array) {
        return x.length;
    } else {
        return 0;
    } 
}

OMG... Took me atleast 30 minutes but!!!!

function arrayCounter (myArray) { if (typeof myArray === 'string' ||typeof myArray === 'number' ||typeof myArray === 'undefined') { return 0; } return myArray.length; } //I got it!

Took a little bit but got it: function arrayCounter(myArray) { if(typeof myArray==='string'){ return 0; } if(typeof myArray==='number'){ return 0; } if(typeof myArray==='undefined'){ return 0; } else { return myArray.length;} } Hope this helps

           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;

      }
      }
            ```

This worked for me and uses everything learned in this lesson.

Hey Erik, instead of using multiple "if" statements you should use "if else" statements for clearer code or you should explore the logical operator " || " to make your code DRY and easier to read and understand. For example;

     function arrayCounter(x) {

     if(typeof x !== 'undefined' || typeof x !== 'number' || typeof x !== 'string')

         return x.length;

     } else {

         return 0;

     }

In the above example I use both logical operators " || " and " !== "; the first operator I have listed ( || ) means " or ", this can be used to slim down your conditional statements as shown above instead of using multiple statements. Also the second operator I have listed ( !== ) means " is not equal to the type "; again this can be used to check to see if the array isn't a number which means it will return the length and not 0.

Hey guys,

This is the method i used, and seemed to have worked.

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

but after solving the problem, in order to write clean and readable code, i should have done it this way.

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

             else{
                return xyz.length;
             }
      }

Both should anyways work, but it is always best to follow best practices.

Here's the long way:

 function arrayCounter(myArray) {
   if (typeof myArray === 'string') {
    return 0;

   }else if (typeof myArray === 'number') {
    return 0;

   }else if (typeof myArray === 'undefined') {
    return 0;

   }else {
    return myArray.length;
   }
}

Great discussion so far...

I understand how the most recent answer works, but what if "typeof myArray === boolean"? That would not be a string, a number, or undefined, so wouldn't the function try to return the code that is in the else statement (which would lead to an error because it is trying to determining the length of a boolean)?

Thanks, Mike

Hey i just tried this code and works pretty smooth.

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

Another option :

function arrayCounter(x){
        if (Array.isArray(x) === true){
          return x.length;
        }else{
          return 0;
        }
      }