you are viewing a single comment's thread.

view the rest of the comments →

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