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

Caroline Rawson
Caroline Rawson
2,174 Points

Javascript foundations, Functions, Return values challenge

I am getting an error for the Return Values challenge and I don't understand why.

Here's my code:

    function arrayCounter (function_array) {
        if (function_array.isArray){
        return function_array.length;
        }
        if (typeof function_array === ('string' | 'number' | 'undefined')) {
        return 0;
        }
      }  

I am getting the following error: Bummer! You're not checking if 'undefined' is being passed in

Why is my check for undefined not working?

thanks!

Caroline Rawson
Caroline Rawson
2,174 Points

Ok I realised that my OR operators were incorrect...

My use of isArray() was also incorrect - so I guess the first bit of code was being ignored and that's why the error related to the checking of undefined, which threw me a bit! Fixed these and now it's working :-)

My answer:

    function arrayCounter (function_array) {
    if (Array.isArray(function_array)){
        return function_array.length;
        }       
  if (typeof function_array === 'string' || typeof function_array === 'number' || typeof function_array === 'undefined') {
        return 0;
        }
      }   

Great that you found the solution yourself. For future ref i found another post here with the same question answered link