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

Kim Stockley
Kim Stockley
6,835 Points

Code Challenge: JavaScript, Return Values

If anyone can give me a clue as to how what I'm doing wrong on this Code Challenge on Return Values (JavaScript Deep Dive) I would be forever grateful. I've tried a million different code combinations and rewatched the videos multiple times. Ready to put this challenge behind me. Thank you!

Q: "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."

My most recent attempt: http://s27.postimg.org/ro7fszmqb/returnvalue.png

3 Answers

Update

As I said previously, your function didn't make much sense. This is what I would do:

// declare an array to pass to the function
var my_array = [1,2,3];

// this function takes an argument - my_arg
function arrayCounter(my_arg) {
  // here we check if the variable is an array.
  // if it is, return the length. if not, return 0.
  if (my_arg instanceof Array) {
    return my_arg.length;
  } else {
    return 0;
  }
}

// we must remember to call the function
arrayCounter(my_array);
Kim Stockley
Kim Stockley
6,835 Points

Hey Eirik,

Thanks for trying to help! I apologize that my code is complete gibberish. It feels like I've tried everything. Not sure if this will help, but when I tried the code in your previous comment, I got " Can't find variable: array".

Yes, I made a complete mess. I'm sorry about that. I updated it, so you can try it now.

Kim Stockley
Kim Stockley
6,835 Points

That worked! Thank you SO much.

The most frustrating part is that the videos leading up to the challenge didn't really cover what the challenge was looking for. Thanks again, Eirik!

Great, glad to help :)

Yeah thanks Erik, you made that really easy to understand.

Kim, I agree. I've found both the CSS and Javascript deep dives to be way too theoretical. They sure are thorough, they seem to cover most of the foundational stuff, but just not in a way that I can really comprehend and start using. For Javascript I found the book Javascript & jQuery: The Missing Manual by David Sawyer McFarland to be a god send, everything is explained super clearly with really practical tutorials. After reading just a third of that book I felt I was ready to start doing some Javascript, and I found the stuff in this Javascript deep dive started to make a lot more sense.