Javascript function call with another function as parameter

cwendel

I have a few functions in two different files that are all linked together by function calls they are as follows

FILE 1:

function getFunction(func){

}   

FILE 2:

function Numbers(one, two) {
    return (one*two);
}


var func = getFunction(Numbers);

and these are called by:

func(input_array);

my array has values 1,3,5,7,9 and I need func(input_array) to return 3,15,35,63,9 (the last value loops back to the first value)

basically what I am trying to do is have getFunction return a function such that these values are calculated. I am having trouble because I can't wrap my mind about sending and returning functions. I don't know how to access the array if it isn't sent into the function. Let me know if I need to clarify anything.

Barmar
function getFunction(callback) {
    return function(array) {
        return array.map(function(cur, index) {
            return callback(cur, array[(index+1) % array.length]);
        });
    };
}

getFunction returns a closure over the callback parameter, which is the function that you want to call. The closure receives the array parameter, and it calls the callback in a loop over the array using array.map. The % modulus operator performs the wraparound that you want.

Another way to write this that may be clearer is:

function getFunction(callback) {
    return function(array) {
        var result = [];
        for (var i = 0; i < array.length; i++) {
            j = (i+1) % array.length; // Next index, wrapping around
            result.push(callback(array[i], array[j]));
        }
        return result;
    };
}

var func = getFunction(Numbers);
console.log(func([1,3,5,7,9])); // Logs [3,15,35,63,9]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Call a parameter of a function in another one

From Dev

Is it possible to call a function as a parameter of another function in Swift?

From Dev

javascript anonymous function call with parameter

From Dev

Another way to call function javascript

From Dev

A javascript function that complete another function sent as a parameter?

From Dev

Javascript - Return in a function parameter another function

From Dev

How to call a function within another function in javascript

From Dev

How to call a function within another function in javascript

From Dev

Cannot call a function inside another function javascript

From Dev

How to call a function that is inside another function javascript

From Dev

Call a function in viewcontroller with parameter from another class

From Dev

How to call a function another function as a parameter passed as a string

From Dev

Call a function that has parameter type inside another function c#

From Dev

Can I pass a function call as a parameter to another function? Python

From Dev

Call a function that has parameter type inside another function c#

From Dev

how to call a function which is passed as parameter in another function

From Dev

Function call in another function using same parameter received

From Dev

Call php function from javascript and send parameter

From Dev

How to call a JavaScript function with parameter in jQuery?

From Dev

How to call a JavaScript function with parameter in jQuery?

From Dev

Javascript pass parameter to a call back function

From Dev

Please explain the javascript: parameter in function call

From Dev

How to call a function of another window in javascript?

From Dev

In Scala, is a function that takes call by name parameter different from a function that takes another function as parameter?

From Dev

In Scala, is a function that takes call by name parameter different from a function that takes another function as parameter?

From Dev

How to call a function with a function as parameter?

From Dev

JavaScript – Call a function of the prototype from another prototype function?

From Dev

How to test the function call count of another function in Javascript?

From Dev

How to test the function call count of another function in Javascript?

Related Related

  1. 1

    Call a parameter of a function in another one

  2. 2

    Is it possible to call a function as a parameter of another function in Swift?

  3. 3

    javascript anonymous function call with parameter

  4. 4

    Another way to call function javascript

  5. 5

    A javascript function that complete another function sent as a parameter?

  6. 6

    Javascript - Return in a function parameter another function

  7. 7

    How to call a function within another function in javascript

  8. 8

    How to call a function within another function in javascript

  9. 9

    Cannot call a function inside another function javascript

  10. 10

    How to call a function that is inside another function javascript

  11. 11

    Call a function in viewcontroller with parameter from another class

  12. 12

    How to call a function another function as a parameter passed as a string

  13. 13

    Call a function that has parameter type inside another function c#

  14. 14

    Can I pass a function call as a parameter to another function? Python

  15. 15

    Call a function that has parameter type inside another function c#

  16. 16

    how to call a function which is passed as parameter in another function

  17. 17

    Function call in another function using same parameter received

  18. 18

    Call php function from javascript and send parameter

  19. 19

    How to call a JavaScript function with parameter in jQuery?

  20. 20

    How to call a JavaScript function with parameter in jQuery?

  21. 21

    Javascript pass parameter to a call back function

  22. 22

    Please explain the javascript: parameter in function call

  23. 23

    How to call a function of another window in javascript?

  24. 24

    In Scala, is a function that takes call by name parameter different from a function that takes another function as parameter?

  25. 25

    In Scala, is a function that takes call by name parameter different from a function that takes another function as parameter?

  26. 26

    How to call a function with a function as parameter?

  27. 27

    JavaScript – Call a function of the prototype from another prototype function?

  28. 28

    How to test the function call count of another function in Javascript?

  29. 29

    How to test the function call count of another function in Javascript?

HotTag

Archive