Created
July 30, 2021 15:35
-
-
Save astagi/f35e0d8472c792e027e17648517c2b54 to your computer and use it in GitHub Desktop.
Get elements between even numbers using only reduce
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 elementsBetweenEvenNumbers = (arr) => { | |
return arr.reduce((x, current) => { | |
x[0].push(current) | |
if (x[0].length === 3) { | |
if (x[0][0] % 2 === 0 && x[0][2] % 2 === 0) | |
x[1].push(x[0][1]) | |
x[0] = x[0].slice(1) | |
} | |
return x | |
}, [[],[]])[1] | |
} | |
console.log(elementsBetweenEvenNumbers([1, 2, 3, 4, 5, 6, 7])) | |
console.log(elementsBetweenEvenNumbers([55, 92, 4, 96, 11, 4, 1, 2])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment