Skip to content

Instantly share code, notes, and snippets.

@GoodNovember
Created September 27, 2019 19:18
Show Gist options
  • Select an option

  • Save GoodNovember/7c871810b75d413675e01f8a7cf08074 to your computer and use it in GitHub Desktop.

Select an option

Save GoodNovember/7c871810b75d413675e01f8a7cf08074 to your computer and use it in GitHub Desktop.
const differentiate = (targetArray, filterFunction) => targetArray.reduce(([truthAcc, falseAcc], item, index, arr) => {
if (filterFunction(item, index, arr)) {
truthAcc.push(item)
} else {
falseAcc.push(item)
}
return [truthAcc, falseAcc]
}, [[], []])
module.exports = { differentiate }
const { differentiate } = require('./differentiate.js')
test('Differentiate Works', () => {
const inputArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
const expectedEvenValues = [0, 2, 4, 6, 8, 10]
const expectedOddValues = [1, 3, 5, 7, 9]
const isEvenArrayFilter = number => number % 2 === 0
const [evenValues, oddValues] = differentiate(inputArray, isEvenArrayFilter)
expect(evenValues).toEqual(expect.arrayContaining(expectedEvenValues))
expect(oddValues).toEqual(expect.arrayContaining(expectedOddValues))
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment