I need to write a function that loops through an array of numbers, and returns the odd & even numbers in it's array.
I'm not sure if there's a better way to do this, and I'm stuck. Is there a way to return both statements?
var myNums = [1, 2, 3, 4, 5, 6, 7, 9];
var evens = [];
var odds = [];
function oddsAndEvens(nums) {
for(var i = 0; i < nums.length; i++){
if(nums[i] % 2 === 0){
evens.push(nums[i])
}
else if (!nums[i] % 2 === 0) {
odds.push(nums[i])
}
}
console.log(evens);
console.log(odds);
//I need it to "return" the array,
//not console log
}
console.log(oddsAndEvens(myNums));
elserather than anelse if. If it's not even, it can only be odd!