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

I'm stuck again.... Please help..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.

13 Answers

Here... try this. x is my array.

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

yea that's what I was thinking but that would just seem dumb to have to test it 3 times... and I didn't know about the or operator either.... Thanks for your help man... Seriously.. I appreciate it

Orlando, Write your function definition as described above. Inside your function, use the typeof operator to test the argument for 'string', 'number' and 'undefined'. If the test comes back as a true match for any one, then return 0; otherwise, return the length of the array. I hope that helps.

It helps to create your own index.html, write the function in that and open in browser. Then use the JavaScript console to debug your code.

Thanks for your help... But I'm still lost... This is what I wrote and I don't understand what I'm doing wrong.... function arrayCounter (myArray) { if (typeof myArray === 'undefined'){ return "0"; } myArray.length; };

You're not returning the length of the array; missing 'reutrn'

Good work. You just need to test for 'string' and 'number' types as well.

You're not returning the length of the array either, you have no return statement outside of the if block.

Sorry fellas, but I'm TOTALLY lost.... I still don't understand what you guys mean... Thanks for your help tho

Well, you're only testing for 'undefined', you need to ensure that it isn't a string, nor a number as well. It would be easier to check to see if the argument is an array: if (myArray.constructor == Array)...

inside this if, you can return myArray.length.. else, return 0

That does make sense but I haven't learned about ".constructor" so I wouldn't even begin to know how to use that or that it was even a possibility.

Well that was just my personal suggestion, rather than having to test for each individually.. it's not covered in the videos but it will still pass the test,

alternatively, you could have || (or) / && (and) operators, or even nested ifs/else ifs (don't recommend it) to test for all 3

Hope it helps man.

Thanks everyone for your help... Those worked and made sense but they didn't teach that in the video so I wonder what answer they were really looking for.....????

Honestly, they're not really looking for any one way, really, you could accomplish the same effect via many different means. Their test is likely to include a call to your method with different arguments (first they will pass a number, then a string, then 'undefined, and lastly an array, if all return the expected values.. it will let you continue on to the next challenge). I agree in that much of it hasn't been covered; perhaps they were looking for you to use a couple of if/if else's? just stick with it, it gets easier.

Aight cool... Thanks man.