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

Hi, could you please tell me what's wrong with this answer

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

2 Answers

Andrew Shook
Andrew Shook
31,709 Points

I have answered this one a few times. Take a look here for an explanation of the problem.

Matthew Reed
Matthew Reed
Courses Plus Student 17,986 Points

replace the whole statement typeof a === 'array' with Array.isArray(a)

Logan R
Logan R
22,989 Points
function arrayCounter (a) { 
        if (Array.isArray(a)) { 
          return a.length; 
        } else { 
          return 0;
        };

Tried it and that doesn't work.

Matthew Reed
Matthew Reed
Courses Plus Student 17,986 Points

Actually I guess you have to check if it's an object like Andrew Shook said in the link he posted

Logan R

You're missing a closing curly brace which causes a syntax error. If you add it in then that code will pass the challenge.

The isArray method would be the most foolproof way to check for an array that I know of. However, you don't learn it in the course and you can't really be expected to know it for this challenge.

Matthew Reed
Matthew Reed
Courses Plus Student 17,986 Points

Jason Anello I thought that was true about isArray(). Nice to see I haven't gone crazy :/

Andrew Shook
Andrew Shook
31,709 Points

the isArray method is the best choice in the real world. However, the JS course doesn't go over isArray(), but instead explains typeof. That is why I have, on several occasions, to answer the question using typeof. It isn't the best way to check for an array, but it does at least stay in sync with that is being taught.

Andrew,

I agree with you that you should give solutions based on what is taught so far but I think it's ok to post better solutions so we can all learn more provided you've also shown what the course is based on.

I've looked over your solution but was it taught up to this point that the type of an array is an "object"? You would need to know that in order to come up with your solution and your solution doesn't match up with the challenge instructions.

I think it would be better to explicitly check the type for "string", "number", or "undefined" and return 0 in any of those cases. Otherwise, assume it's an array and return it's length. This is what the challenge instructions are leading you to do.

Something like this:

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

        //Assume it's an array
        return array.length;
      }

It can be improved a little with the logical OR operator, ||, but I don't think that was taught either up to this point.

Sean T. Unwin
Sean T. Unwin
28,660 Points

I wanted to mention that Array.isArray() is not compatible with <=IE8. A more legacy proof solution would be:

function arrayCounter(arr) {
  if(arr instanceof Array) {
    return arr.length;
  } else { return 0;}
}
Aaron Graham
Aaron Graham
18,033 Points

Sean T. Unwin - You can also just polyfill the Array.isArray() function:

if (!Array.isArray) {
  Array.isArray = function(arg) {
    return Object.prototype.toString.call(arg) === '[object Array]';
  };
}

Straight from MDN

Sean T. Unwin
Sean T. Unwin
28,660 Points

True, Aaron, but then that`s more code. Why do I want to type more code? ;-D

Aaron Graham
Aaron Graham
18,033 Points

Sean T. Unwin - It is my understanding that Array.isArray() is the suggested method for checking if a variable is an array in ECMA 5. There are some edge cases where other methods fail (i.e. moving arrays across multiple frames). Using polyfills allows you to code to modern browser standards and not lose backward compatibility. It's really a personal choice, I just wanted to mention that this method was available.

Sean T. Unwin
Sean T. Unwin
28,660 Points

That's my understanding as well and I agree with you on the use of and reason for polyfills. I suppose I was a tad flippant previously. However, within a single DOM evnvironment I probably stand by my suggestion if legacy support is to be a consideration.

When there are multiple considerations it does appear the best answer, in terms of cross-browser/legacy support, support across multiple frames and the least amount of code (all 3 points), would be to simply use the polyfill:

if (Object.prototype.toString.call(obj) === '[object Array]') { }

Although, in terms of going forward and not looking back to older browsers the best answer is Array.isArray().

Thanks for the explanation, Aaron Graham. Good points all around and I had a good time researching it a little more deeply.

Btw, this is a detailed article on the subject - http://perfectionkills.com/instanceof-considered-harmful-or-how-to-write-a-robust-isarray/