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

Brian Schmitz
Brian Schmitz
11,167 Points

Using the function to find the length of the array

Can someone please help me with this challenge, my code is below. Trying to return 0 if the value is undefined, a string, or a number, otherwise print the length of array back to the console. Here is my code:

function arrayCounter (array) { if (typeof array === 'undefined' || [1,2,3] || 'string') { return 0; return array.length; } }

Help is appreciated, thanks

2 Answers

You need to do

function arrayCounter(array) {
    if (typeof array === 'undefined' || typeof array === 'number' || typeof array === 'string') {
        return 0;
    }

    else {
        return array.length;
    }
}

You have to do "typeof array ===" every time between the OR symbols, as each one is a different comparison.

Brian Schmitz
Brian Schmitz
11,167 Points

Thanks Bryan. Have you figure out this challenge as well - getting the "window.didExecute = true " statement to execute within the anonymousFunction? If so, any help would be appreciated.

var anonymousFunction = function (){ }; (function () { window.didExecute = true; });

I sure do have the answer for that question as well.

var anonymousFunction = function() {};

      (function () {
        window.didExecute = true;
      }) ();

I recommend heading back to the anonymous functions video. I know I will, cause I don't quite understand it all :P Good luck.

Check out the explanations here for how an anonymous function works http://stackoverflow.com/questions/1140089/how-does-an-anonymous-function-in-javascript-work. The 3rd one is pretty good.