Created
September 18, 2020 19:54
-
-
Save djD-REK/a6c16202a8d2a441f1f750f883853476 to your computer and use it in GitHub Desktop.
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 addWeirdStuffRefactor = (arrayOne, arrayTwo) => { | |
const sumOfEvensInArrayTwo = arrayTwo.reduce( | |
(sumOfEvens, itemTwo) => | |
itemTwo % 2 === 0 ? sumOfEvens + itemTwo : sumOfEvens, | |
0 // Start with an initial value of 0 | |
) | |
const sumOfOddsInArrayTwo = arrayTwo.reduce( | |
(sumOfOdds, itemTwo) => | |
itemTwo % 2 === 1 ? sumOfOdds + itemTwo : sumOfOdds, | |
0 // Start with an initial value of 0 | |
) | |
const outputArray = [] | |
for (const itemOne of arrayOne) { | |
// Add sum of odds if itemOne is less than 10; add sum of evens if >= 10 | |
let outputValue = | |
itemOne < 10 | |
? itemOne + sumOfOddsInArrayTwo | |
: itemOne + sumOfEvensInArrayTwo | |
// Add one to each itemOne if arrayTwo contains an element greater than 20 | |
outputValue += arrayTwo.some((itemTwo) => itemTwo > 20) ? 1 : 0 | |
outputArray.push(outputValue) | |
} | |
return outputArray | |
} | |
console.log("------------") | |
console.log( | |
`[${addWeirdStuffRefactor( | |
[1, 3, 5, 17, 15], | |
[1, 2, 3, 4, 5] | |
)}] should equal [10,12,14,23,21]` | |
) | |
console.log( | |
`[${addWeirdStuffRefactor( | |
[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