Array-Function "oddsAndEvens" loops a parameter "nums"

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);
  }
}

What is Array ?

An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. After creation, its length is fixed.


Learn More :