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

Alex Bukreev
Alex Bukreev
8,362 Points

Return Values

Hi guys! That challenge task simply overwhelms my head =8-)

So as i understand, completion of the task is comprised of giving 1) an (if) statement that returns the length of the array (Q - should i pass in some random values to it?) 2) another (if) that returns "0" in case one of that three values is passed in (Question - in array?) ThanX!!!

3 Answers

J Scott Erickson
J Scott Erickson
11,883 Points

If I recall the task correctly from when I took the course you need something like this:

if (typeof(var) === 'string' || typeof(var) === 'number' || typeof(var) === 'undefined') {
  return 0;
} else {
  return var.length;
}
Michelle Cannito
Michelle Cannito
8,992 Points

typeof is an operator, not a method. Your example shows incorrect syntax. Correct example of using typeof: if (typeof parm_array == "undefined")

As an aside; in this case, you do not need the else. The if being true will execute the return of zero and exit the function. The if being false will drop down to execute the next instruction, which is also a return statement.

Here is the wording of the challenge: "...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."

Alex Bukreev
Alex Bukreev
8,362 Points

Thanks folks! here's what is considered to be a right code by the way, "Or" operator is essential, this one — "||" , pay attention ))

function arrayCounter (myarray)

  {if (typeof(myarray) === 'string' ||
       typeof(myarray) === 'number'  ||
       typeof(myarray) === 'undefined')
  {

return 0;} else { return myarray.length;}}