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

Michael Bardeggia
Michael Bardeggia
1,659 Points

Stumped on this one...

Here is what I have:

It says: Bummer you're missing a function called arrayCounter!

Please help.

      function arrayCounter(passin) {
        if passin typeOf === Array {
          return passin.length;
        }
        return 0;
      }
      arrayCounter();

5 Answers

geoffrey
geoffrey
28,736 Points

I have the answer, that's simple, you have to check with instanceof. I tested using the console log of chrome and that seems to work perfectly, it passed the exersise as well on TH.

 function arrayCounter(myVar){ 

   if( myVar instanceof Array){ 

     return myVar.length; 
   } else { 

     return 0; 
   } 

 } 

var thevalue = ["Bob", "Jack", "Bill", "Jane"];

As I haven't seen yet all the videos I don't know if the teacher talks about that, probably, but well, that works.

Edit: I've just seen a staff member posted the same code as solution in the previous posts... lol we've looked for the answer till now, almost for nothing :-p.

Check the previous posts to have more info.

Enara L. Otaegi
Enara L. Otaegi
13,107 Points

When you define the function your passing an argument to it, that is passin, but when you call it it's empty. Could it be the problem?

geoffrey
geoffrey
28,736 Points

I've just tested and I have the same issue. It tells the same error. Can't help, sorry. But about the code you typed, I think there is a mistake with the way you use typeof, I think you spell it wrongly.

If I remember well, even if I haven't reached this exersise yet, that should be something like that:

    function arrayCounter(myVar){

        if(typeof myVar.isArray){
          return myVar.length;
      }
        else{
          return 0;
        }


        var table = ["something","cool"];

       arrayCounter(table); 

But that doesn't help much more, sorry.

Michael Bardeggia
Michael Bardeggia
1,659 Points

For question: 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.

Based on your feedback above I've tried (but I'm still getting errors)...

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

function arrayCounter(myVar){ 
  if(typeof myVar === Array){ 
    return myVar.length; } 
  else { 
    return 0; }
}
  var thevalue = ["Bob", "Jack", "Bill", "Jane"];

  arrayCounter(thevalue);

Any thoughts?

Michael Bardeggia
Michael Bardeggia
1,659 Points

Thanks. You're the man!

instanceof worked!