1/* Answer to: "higher order functions javascript" */
2
3/*
4 Higher order functions are functions that operate on other
5 functions, either by taking them as arguments or by returning
6 them.
7
8 In simple words, A Higher-Order function is a function that
9 receives a function as an argument or returns the function as
10 output.
11
12 For example; Array.prototype.map, Array.prototype.filter and
13 Array.prototype.reduce are some of the Higher-Order functions
14 built into the language.
15
16 For more information, go to:
17 https://blog.bitsrc.io/understanding-higher-order-functions-in-javascript-75461803bad
18*/
1function copyArrayAndManipulate(array, instructions) {
2 const output = []
3 for(let i = 0; i < array.length; i++) {
4 output.push(instructions(array[i]))
5 }
6 return output
7}
8
9function multiplyBy10(input) {
10 return input * 10
11}
12
13copyArrayAndManipulate([1, 2, 3], multiplyBy10)
1// function that adds 2 numbers
2function add(number1, number2){
3 return number1 + number2;
4}
5
6// function that multiplies 2 numbers
7function multiply(number1, number2){
8 return number1 * number2;
9}
10
11// higher order function: takes 2 arguments and a function in this case
12function calc(number1, number2, fn){
13 return fn(number1, number2);
14}
15
16// this is how you would use it
17calc(10, 2, add); // you can also use 'multiply' or any other function
18
19// output: 12
20