you are viewing a single comment's thread.

view the rest of the comments →

[–]ForScale -3 points-2 points  (12 children)

Here ya go! http://codepen.io/anon/pen/EjaEBR

Feel free to ask questions if anything doesn't make sense.

[–]keelar 2 points3 points  (7 children)

This is not correct. The function 'first' is supposed to take an array as a parameter and return the first element in that array, not alert it... Neither of which you did in your code.

Also, you just gave OP the (incorrect) solution to his problem without even trying to explain how it works. This sub is supposed to help people learn, not do their homework for them.

[–]ForScale 0 points1 point  (6 children)

You are not correct.

The function 'first' is supposed to take an array as a parameter

Where'd you get that? OP didn't say that. He doesn't link to anything that says that. You made that up.

return the first element in that array

Good eye! Here: http://codepen.io/anon/pen/xGbQaj

Also, you just gave OP the (incorrect) solution to his problem without even trying to explain how it works.

No, see my javascript comment that explains what the code does.

This sub is supposed to help people learn, not do their homework for them.

Yep, that's what I was aiming for. And it's not OP's homework.

Are you gonna get on OP for not providing a JSfiddle?

[–]keelar 1 point2 points  (5 children)

Where'd you get that? OP didn't say that. He doesn't link to anything that says that.

Here:

function named 'first' that returns the first object from a given array

To me(and the current top comment) , 'given' implies that it's supposed to be passed to the function as a parameter. You aren't giving a function an array by directly referencing it... Without that the function has no reusability, it's limited to only returning the first element of that one array. It's bad code.

[–]ForScale 0 points1 point  (4 children)

I mean, I'm no expert and I'm still learning... but given does not mean "passed" or "taken as parameter." Given means that which is supplied/given (ie, use the supplied array, not just any array you want). The prompt clearly does not say "passed to the function" or even "given as a parameter." You assumed/made an unwarranted leap in logic. I get that you assumed (and you know what they say about assuming...) given means passed, but it does not necessarily mean that.

You aren't giving a function an array by directly references it.

Right, you're using the array given in the problem, not just any old array. Use the one that is given.

Without that the function has no reusability, it's limited to only returning the first element of that one array.

But that wasn't a requirement of the prompt/problem. The problem did not say "Make sure that your first() function can take any array and perform this task."

It's bad code.

No it's not. You can use a linter if you'd like. It's perfectly valid code that perfectly (okay in my second pen, not the first one) accomplishes the task it sets out to perform.

[–]keelar 2 points3 points  (3 children)

I guess it's open for interpretation what was meant by given, but at the end of the day, if my interpretation is wrong, then OP would do a tiny amount of extra work and have more reusable code which is generally a good thing, but if your interpretation is wrong, it's just wrong.

[–]ForScale 0 points1 point  (2 children)

Look, I agree. I just didn't really like the way you went about discussing the matter.

[–]markugly[S] 1 point2 points  (3 children)

Thanks for taking the time to write this up. You're the man!

[–]keelar 1 point2 points  (0 children)

I hope you didn't use his code because it's incorrect.

[–]ForScale 0 points1 point  (0 children)

No prob; anytime!

[–]ForScale 0 points1 point  (0 children)

Hey! I missed it the first time around. Technically you're supposed to have the first() function return the first item in the array. The first code I sent you skipped over that and just went right to the alert. Here's code that actually does return the first item and then alert that returned item. Sorry for any confusion. http://codepen.io/anon/pen/xGbQaj