Answers for "higher order functions javascript"

1

higher order functions javascript

// 01 - Array Method .reduce()
// The .reduce() method iterates through an array and returns a single value.

// 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.

// Here, the .reduce() method will sum all the elements of the array 

const arrayOfNumbers = [1, 2, 3, 4];

const sum = arrayOfNumbers.reduce((accumulator, currentValue) => {  
  return accumulator + currentValue;
});

console.log(sum); // 10

/////////////

// 02-  Array Method .forEach()
// The .forEach() method executes a callback function on each of the elements in an array in order.

// Here, the callback function containing a console.log() method will be executed 5 times, once for each element.

const numbers = [28, 77, 45, 99, 27];

numbers.forEach(number => {  
  console.log(number);
}); 

/////////////

// 03 - Array Method .filter()
// 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.

// Here, the array filteredArray will contain all the elements of randomNumbers but 4.

const randomNumbers = [4, 11, 42, 14, 39];
const filteredArray = randomNumbers.filter(n => {  
  return n > 5;
});

/////////////

// 04 - Array Method .map()
// 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.

// The original array does not get altered, and the returned array may contain different elements than the original array.

const finalParticipants = ['Taylor', 'Donald', 'Don', 'Natasha', 'Bobby'];

const announcements = finalParticipants.map(member => {
  return member + ' joined the contest.';
})

console.log(announcements);
Posted by: Guest on October-14-2021
16

higher order functions javascript

/* Answer to: "higher order functions javascript" */

/*
  Higher order functions are functions that operate on other
  functions, either by taking them as arguments or by returning
  them.
  
  In simple words, A Higher-Order function is a function that
  receives a function as an argument or returns the function as
  output.
  
  For example; Array.prototype.map, Array.prototype.filter and
  Array.prototype.reduce are some of the Higher-Order functions
  built into the language.
  
  For more information, go to:
  https://blog.bitsrc.io/understanding-higher-order-functions-in-javascript-75461803bad
*/
Posted by: Guest on April-18-2020
0

js higher order function examples

function copyArrayAndManipulate(array, instructions) {
  const output = []
  for(let i = 0; i < array.length; i++) {
    output.push(instructions(array[i]))
  }
  return output
}

function multiplyBy10(input) {
  return input * 10
}

copyArrayAndManipulate([1, 2, 3], multiplyBy10)
Posted by: Guest on May-22-2021
0

js higher order functions

// function that adds 2 numbers
function add(number1, number2){
	return number1 + number2;
}

// function that multiplies 2 numbers
function multiply(number1, number2){
	return number1 * number2;
}

// higher order function: takes 2 arguments and a function in this case
function calc(number1, number2, fn){
	return fn(number1, number2);
}

// this is how you would use it
calc(10, 2, add); // you can also use 'multiply' or any other function

// output: 12
Posted by: Guest on May-17-2021

Code answers related to "higher order functions javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language