Skip to content

Instantly share code, notes, and snippets.

@djD-REK
Created September 18, 2020 19:54
Show Gist options
  • Save djD-REK/b93a5480e648778fd27284980e937f6b to your computer and use it in GitHub Desktop.
Save djD-REK/b93a5480e648778fd27284980e937f6b to your computer and use it in GitHub Desktop.
const addWeirdStuffMap = (arrayOne, arrayTwo) => {
const [sumOfEvensInArrayTwo, sumOfOddsInArrayTwo] = arrayTwo.reduce(
([sumOfEvens, sumOfOdds], itemTwo) => {
return itemTwo % 2 === 0
? [sumOfEvens + itemTwo, sumOfOdds]
: [sumOfEvens, sumOfOdds + itemTwo]
},
[0, 0] // Start with an initial value of 0 for both sums
)
// Add one to each itemOne if arrayTwo contains an element greater than 20
const addOneToArrayOne = arrayTwo.some((itemTwo) => itemTwo > 20) ? 1 : 0
return arrayOne.map((itemOne) =>
// Add sum of odds if itemOne is less than 10; add sum of evens if >= 10
itemOne < 10
? itemOne + sumOfOddsInArrayTwo + addOneToArrayOne
: itemOne + sumOfEvensInArrayTwo + addOneToArrayOne
)
}
console.log("------------")
console.log(
`[${addWeirdStuffMap(
[1, 3, 5, 17, 15],
[1, 2, 3, 4, 5]
)}] should equal [10,12,14,23,21]`
)
console.log(
`[${addWeirdStuffMap(
[1, 3, 5, 17, 15, 1],
[1, 2, 3, 4, 5, 22]
)}] should equal [11,13,15,46,44,11]`
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment