Created
September 27, 2023 04:38
-
-
Save phptuts/ab8e23340a27026d24206717f0f3fa7a to your computer and use it in GitHub Desktop.
reduce function example day 21
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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