0

I am trying to pass this arr into a function, one element at a time. I have more work to do beyond that, but I cannot even get it to send the element to the square function, so this is just a hurdle that I don't understand? Anyway, this is the code:

var arr = [1,2,3,4];

function square(element){
    return element * element;
}

function applyFunction(arr, square){
    for(var i = 0; i <= arr.length-1; ++i){
        alert(square(arr[i]));
    }
}

applyFunction(arr,square());

Any help would be appreciated, as I am sure this is simple for you guys.

3
  • 10
    Change applyFunction(arr,square()); to applyFunction(arr,square); Commented Jul 8, 2014 at 14:59
  • 1
    square is the function, square() runs the function square and returns the result. Commented Jul 8, 2014 at 15:02
  • @dystroy, please post as answer. jsbin.com/magahima/1/edit Commented Jul 8, 2014 at 15:04

1 Answer 1

1

Modify your last line from

applyFunction(arr,square());

to

applyFunction(arr,square);
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.