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

Stage 5 Functions challenge

I'm confused as to what I am doing wrong here. It took me ages to get to this point but I think my code is finally right. Why does it keep telling me that I'm missing a function named 'arrayCounter'?

I know I can't put '' in a function name so what am I missing?

My code for reference: function arrayCounter (array) { if (typeof array==='string') { return 0;} elseif (typeof array=== 'number') { return 0;} elseif (typeof array==='undefined') { return 0;} else { return object.length;} }

Thanks!

2 Answers

First of all, there is no elseif in JavaScript. What you want to use is else if. Second, you should be returning the length of the variable array, not object as there is no such variable inside the scope of your function (or outside its scope).

Thank you!! I made those adjustments and it helped. I honestly worked on this for way too long and still missed those.

Glad I could help. Now that you're finished, I'd like to show you a much simpler way of doing this (that would also pass the challenge).

function arrayCounter(array) {
  if (array instanceof Array) {  // Check if the array variable really is an array
    return array.length; // If it is, return the length
  }
  return 0; // Return 0 if it's not. There's no need to write an else if/else because this code will be skipped if it's really an array as the `if` returns from the function
}