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

General Discussion

Else and Return

I know how to solve the following question prompt, but I have a question on why an alternate solution won't work. The code challenge is this: 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.

If I just write "return array.length;" after I have used an if-statement to return a value of 0 for 'string', 'number' or 'undefined', I can pass the solution. But I don't understand why it is logically incorrect to write "return array.length" as an else-statement.

In the first case, an if-statement is used, and then if the code doesn't satisfy it, the program will execute with the return array.length. In the second case, an if-statement is used, and if it doesn't satisfy it, the program doesn't execute with the else-statement. Why not? It seems that both would exhaust all logical options. Either the input is a 'string', 'number' or 'undefined' or it isn't. If it is, return 0, if it isn't, return array.length. I don't understand what distinguishes the two, that is, why else won't execute identically to the return.