Skip to content

Instantly share code, notes, and snippets.

@astagi
Created July 30, 2021 15:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save astagi/f35e0d8472c792e027e17648517c2b54 to your computer and use it in GitHub Desktop.
Save astagi/f35e0d8472c792e027e17648517c2b54 to your computer and use it in GitHub Desktop.
Get elements between even numbers using only reduce
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