user3816929
user3816929

Reputation: 1

passing an array into a function from within a function

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.

Upvotes: 0

Views: 63

Answers (1)

ExaMuff
ExaMuff

Reputation: 72

Modify your last line from

applyFunction(arr,square());

to

applyFunction(arr,square);

Upvotes: 1

Related Questions