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

How to check if an array value is a number

Hi all, can someone help me with checking if a value in an array is a number. I am stuck with this challenge. Thanks http://teamtreehouse.com/library/return-values

4 Answers

Andrew Shook
Andrew Shook
31,709 Points

So what the challenge is looking for is a function that checks to see if the parameter passed into the function is an array. So what you need to do is use the "typeof" method to check to see if the parameter is a string, number or undefined. If the parameter is one of those you need to return 0, and if it's not then you need to return the length of the array. Try this code:

var arrayCounter = function(x){
    if(typeof(x) == "object"){
        return x.length;
    }else{
        return 0;
    }
}

If you notice, when I used the typeof method I didn't use "array". This is because in javascript, typeof(array) will return "object" not "array". So when checking to see if something is an array you need to compare it to the string "object". It's just one of those weird things about the language. Hope this helps.

Thanks Andrew, I have not yet succeeded; maybe I am not implementing your suggestion well. I'll let you know how things go. Amo

Andrew Shook
Andrew Shook
31,709 Points

I just double checked my code in the challenge and it passed. Let me know if you are still having problems

Hello Andrew, your code did go through. I am not quite sure what I was doing wrong. After several tries success!!! Thanks a lot. Amouh

Askia Harris
Askia Harris
7,962 Points

i'am so stuck on this i dont know what im doing wrong

Andrew Shook
Andrew Shook
31,709 Points

Can you post your code? That might help determine what your problem is.

Askia Harris
Askia Harris
7,962 Points

function(arrayCounter){ if (typeof arrayCounter === "string") { return 0;} else if(typeof arrayCounter === "name"){ return 0; } else if(typeof arrayCounter === "undefined"){ return 0; } else {return arrayCounter.length;}

  }
Askia Harris
Askia Harris
7,962 Points

function(arrayCounter){ if (typeof arrayCounter === "string") { return 0;} else if(typeof arrayCounter === "name"){ return 0; } else if(typeof arrayCounter === "undefined"){ return 0; } else {return arrayCounter.length;}

  }
Askia Harris
Askia Harris
7,962 Points

function(arrayCounter){ if (typeof arrayCounter === "string") { return 0;} else if(typeof arrayCounter === "name"){ return 0; } else if(typeof arrayCounter === "undefined"){ return 0; } else {return arrayCounter.length;}

  }
Andrew Shook
Andrew Shook
31,709 Points

You have a repeated syntax error with the typeof function. You need to put parentheses around arrayCounter like so; typeof(arrayCounter).

Askia Harris
Askia Harris
7,962 Points

Thanks Andrew but i'am still coming up with a error

Andrew Shook
Andrew Shook
31,709 Points

Replace name with number.