Write a function below called "oddsAndEvens" that loops through a parameter "nums" (an array). For each number in the given array, if it is even, it is added to the evens array, if the number is odd, is added to the odds array.
Question has two parts:
1.Write a function below called "oddsAndEvens" that loops through a parameter "nums" (an array).
2.For each number in the given array, if it is even, it is added to the evens array, if the number is odd, is added to the odds array.
Solution:
var evens = [];
var odds = [];
//code here
var oddsAndEvens = function(nums) {
if (nums % 2 === 0) {
evens.push(nums);
} else if (nums % 2 != 0) {
odds.push(nums);
}
}
Question has two parts:
1.Write a function below called "oddsAndEvens" that loops through a parameter "nums" (an array).
2.For each number in the given array, if it is even, it is added to the evens array, if the number is odd, is added to the odds array.
Solution:
var evens = [];
var odds = [];
//code here
var oddsAndEvens = function(nums) {
if (nums % 2 === 0) {
evens.push(nums);
} else if (nums % 2 != 0) {
odds.push(nums);
}
}