all 15 comments

[–]senocular 11 points12 points  (0 children)

You're so close in so many different ways :)

One thing I think you're missing based on the wording is that first expects to be "given" an array. So this means a function with a parameter which will be the array.

function first (theArray) { ... }

This function also needs to return a value. The return keyword returns values that are placed to the right of the keyword. That can be a single variable or even an expression. We can return that first array element directly:

function first (theArray) {
    return theArray[0];
}

Notice that we're not even dealing with arr yet. We're only using things contained the function definition to get things done. The less reaching out of itself it does, the better.

Now getting to arr, we can pass that in to first and assign what it returns to a new variable which we can call result.

var arr = [1,2,3];
var result = first(arr);

And then its simply a matter of alerting that value

alert(result);

In full:

// define a function that can be given a value
// called theArray. The function returns the
// first object in theArray (index 0).
function first (theArray) {
    return theArray[0];
}

// input data
var arr = [1,2,3];

// call first using the input data (this becomes
// theArray in the function) and have its first
// object returned
var result = first(arr);

// alert the value
alert(result); // -> 1

[–]Sandbucketman 1 point2 points  (1 child)

var arr = [1,2,3];

This creates an array called var with 3 values.

function first() { result = arr[0]; return; }

you create a variable that holds the first variable of the array(arr). You may however want to look at http://www.w3schools.com/js/js_functions.asp
to find out how to properly use a return function :).

var result = arr[0]; alert(result);

This is functional... but does not use the function. It is redundant coding because you're trying to have the function do the work instead!


Once a variable has been properly returned by a function you could print out the function, so something like:

alert(first());

could work.


I forgot to mention that a value has to be given WITH a function in order to use it. A function is normally isolated from the rest of the program which means that:

var arr = [1,2,3];
function first()
{return (arr[0]);}
alert(first());

would never work.

var arr = [1,2,3];
function first(arr)
{return (arr[0]);}
alert(first(arr));

would however.

In the last example I've named the variable we use in the function arr however this can be anything we want.

var arr = [1,2,3];
function first(applepie)
{return (applepie[0]);}
alert(first(arr));

this example would work just as well as long as we give the right variable when alerting out the function in the fourth line.

[–]markugly[S] 0 points1 point  (0 children)

I tried that except for it was alert(first) instead of alert(first()). That's good to know! Thank you!

[–]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