Created
February 21, 2020 07:34
-
-
Save alegut/434e17b6d567b3afad9ab0bd06e72d49 to your computer and use it in GitHub Desktop.
Separate Odd and Even Numbers from the Array
This file contains 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 numbers = [1,2,3,4,5,6,7,8,9]; | |
const { evenNumbers, oddNumbers} = numbers.reduce((obj,number) => { | |
if( number % 2 === 0){ | |
obj.evenNumbers.push(number); | |
}else{ | |
obj.oddNumbers.push(number); | |
} | |
return Object.assign({},obj); | |
},{ | |
evenNumbers : [], | |
oddNumbers : [] | |
}); | |
console.log(evenNumbers); // (4) [2,4,6,8] | |
console.log(oddNumbers); // (5) [1,3,5,7,9] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment