Skip to content

Instantly share code, notes, and snippets.

@phptuts
Created September 27, 2023 04:38
Show Gist options
  • Save phptuts/ab8e23340a27026d24206717f0f3fa7a to your computer and use it in GitHub Desktop.
Save phptuts/ab8e23340a27026d24206717f0f3fa7a to your computer and use it in GitHub Desktop.
reduce function example day 21
const numbers = [4,5,9,10];
function reducerSum(acc, next) {
return acc + next;
}
function reducerDouble(acc, next) {
acc.push(next * 2);
return acc;
}
function reducerEven(acc, next) {
if (next % 2 == 0) {
acc.push(next);
}
return acc;
}
const sum = numbers.reduce(reducerSum, 0);
const doubleArray = numbers.reduce(reducerDouble, []);
const evenArray = numbers.reduce(reducerEven, []);
console.log(evenArray, 'evenArray');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment