Recursion in array to find odd numbers and push to new variable JavaScript

We are required to write a recursive function, say pushRecursively(), which takes in an array of numbers and returns an object containing odd and even properties where odd is an array of odd numbers from input array and even an array of even numbers from input array. This has to be done using recursion and no type of loop method should be used.


const arr = [12,4365,76,43,76,98,5,31,4];
const pushRecursively = (arr, len = 0, odd = [], even = []) => {
   if(len < arr.length){
      arr[len] % 2 === 0 ? even.push(arr[len]) : odd.push(arr[len]);
      return pushRecursively(arr, ++len, odd, even);
   return {

While the len variable reaches the end of array, we keep calling the function recursively, each time pushing the odd values to odd array and the evens to even array and once the len variable equals the length of array, we exit out of the function returning the object.


The output of this code in the console will be −

{ odd: [ 4365, 43, 5, 31 ], even: [ 12, 76, 76, 98, 4 ] }