1A “higher-order function” is a function that accepts functions as parameters and/or returns a function.
2
3 Array Method .reduce()
4 Array Method .forEach()
5 Array Method .filter()
6 Array Method .map()
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*/
1// 01 - Array Method .reduce()
2// The .reduce() method iterates through an array and returns a single value.
3
4// It takes a callback function with two parameters (accumulator, currentValue) as arguments. On each iteration, accumulator is the value returned by the last iteration, and the currentValue is the current element. Optionally, a second argument can be passed which acts as the initial value of the accumulator.
5
6// Here, the .reduce() method will sum all the elements of the array
7
8const arrayOfNumbers = [1, 2, 3, 4];
9
10const sum = arrayOfNumbers.reduce((accumulator, currentValue) => {
11 return accumulator + currentValue;
12});
13
14console.log(sum); // 10
15
16/////////////
17
18// 02- Array Method .forEach()
19// The .forEach() method executes a callback function on each of the elements in an array in order.
20
21// Here, the callback function containing a console.log() method will be executed 5 times, once for each element.
22
23const numbers = [28, 77, 45, 99, 27];
24
25numbers.forEach(number => {
26 console.log(number);
27});
28
29/////////////
30
31// 03 - Array Method .filter()
32// The .filter() method executes a callback function on each element in an array. The callback function for each of the elements must return either true or false. The returned array is a new array with any elements for which the callback function returns true.
33
34// Here, the array filteredArray will contain all the elements of randomNumbers but 4.
35
36const randomNumbers = [4, 11, 42, 14, 39];
37const filteredArray = randomNumbers.filter(n => {
38 return n > 5;
39});
40
41/////////////
42
43// 04 - Array Method .map()
44// The .map() method executes a callback function on each element in an array. It returns a new array made up of the return values from the callback function.
45
46// The original array does not get altered, and the returned array may contain different elements than the original array.
47
48const finalParticipants = ['Taylor', 'Donald', 'Don', 'Natasha', 'Bobby'];
49
50const announcements = finalParticipants.map(member => {
51 return member + ' joined the contest.';
52})
53
54console.log(announcements);
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
1A “higher-order function” is a function that accepts functions as parameters and/or returns a function.
2
3 Array Method . reduce() The . ...
4 Array Method . forEach() The . ...
5 Array Method .filter() The . filter() method executes a callback function on each element in an array. ...
6 Array Method .map()