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

Craig Curtis
Craig Curtis
19,985 Points

I'm not sure how to make the function return the length of my array. I get error saying array passed in return value 0.

Another possible problem is I'm doing If (typeof myArray === 'string', 'number', 'undefined) This doesn't seem right. I'm wondering if that is causing an error too,

Craig Curtis
Craig Curtis
19,985 Points

If unclear, I'm doing the Return Values Code Challenge.

3 Answers

Hugo Paz
Hugo Paz
15,622 Points

Try your if like this

if(typeof myArray === 'string' || typeof myArray==='number' || typeof myArray==='undefined)

Craig Curtis
Craig Curtis
19,985 Points

Thanks much! Actually I tried that out before your posting and it worked!

Craig Curtis
Craig Curtis
19,985 Points

I just solved it...

I didn't realize it but the typeof actually recognizes 'undefined', 'string', and 'number' as valid data types. I initially assumed it would be looking for integers or floats, thus I made conditions like if (myArray <= 0 || myArray >= 0). Next, another failed code was checking if (typeof myArray === 'undefined', 'string', 'number'). So the commas didn't work inthe conditional evaluation. In the end, a simple use of verticle bars worked. And I'm sure this is not the best code as it isn't DRY (typeof myArray === 'undefined || typeof myArray === 'string' || typeof myArray === 'number'). This is ugly in my view, but it worked. Anyone know of a more elegant syntax?

Fernando Ramos
Fernando Ramos
4,369 Points

Still not sure how you did this... here is what I typed... Can you please help? It would help if the instructors went over the piped " || " use in videos... I lost so much time with using commas... Thanks!

  function arrayCounter (myArray) {
    if (typeof myArray === 'undefined' || typeof myArray === 'string' || typeof myArray === 'number') {
        return 0;
  }
    return arrayCounter.length;
  }
Craig Curtis
Craig Curtis
19,985 Points

Thanks Fernando, that's a great idea. Just a bit more guidance would be helpful. Possibly a hint section for the site would be good. I was thinking initially just now sacrificing one point to "buy" a int, but rather a different parallel virtual currency would be more ideal. This way, we wouldn't be "punished" for looking at hints.

Likewise, asking and answering postings could also be rewarded with this virtual "Tree Coin" or "Leaves".

Fernando Ramos
Fernando Ramos
4,369 Points

Turns out the code was fine, except the last part or "return"... that should have read:

return myArray.length; and not:

return arrayCounter.length;

Thanks for your reply.