-1

Given the following array:

const arr = [1, [2, 3], [[4, 5], [6], [7, [8, 9]]]]

I need to return a new flat array of numbers [1,2,3,4,5,6,7,8,9]

  • Notes:

    1.Not allowed to use the 'reduce' function. 
    
    2.Not allowed to defining an external array.
    

Here is my partial solution where I print the elements separately. How can I actually return the new array based on the requirements above?

 function flat(arr) {
      if (Array.isArray(arr)) {
        arr.forEach(a => {
          flat(a)
        });
      }
      else {
        console.log(arr)
      }
    }
    flat(arr)
Aim
  • 1
  • 2

0 Answers0