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

Mark Bradshaw
PLUS
Mark Bradshaw
Courses Plus Student 7,658 Points

I'm getting a syntax and parse error on a javascript quiz question. Please assist.

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.

Here's my answer:

    function arrayCounter (["one", "two", "three", "four"]) { return 'undefined';}

1 Answer

Dave McFarland
STAFF
Dave McFarland
Treehouse Teacher

Mark,

When you create a function you add parameter names inside the parentheses like this

function arrayCounter( anArray ) {
  // put the function programming in here
}

Then, when you "call" the function to run the programming inside the function, you can pass the function an "argument":

arrayCounter( [ 1, 2, 3] );

For this challenge you just need to create the function, not call it. So start by using this code:

function arrayCounter( anArray ) {
  // put the function programming in here
}

and seeing if you can add the programming inside to determine if the argument passed to the function is a string, number, undefined or is an array. You can use the typeof operator to determine whether a variable is holding a string, number or undefined value. For example:

var a = "hello";
if (typeof a === "string") {
  return 0;
}

Hope that helps.

Mark Bradshaw
Mark Bradshaw
Courses Plus Student 7,658 Points

Cheers Dave! This is a huge help!!!! Thanks for taking the time to break this down further for me.

Mark Bradshaw
Mark Bradshaw
Courses Plus Student 7,658 Points

Dave, can you explain why I was getting a syntax and parse error?