Coding Challenge Practice – Question 23

The task is to implement the Array.prototype.flat(), which reduces nesting of an array.

The boilerplate code:

function flat(arr, depth = 1) {
// your implementation here
}

The flat method receives an array and a number to determine how many…


This content originally appeared on DEV Community and was authored by Bukunmi Odugbesan

The task is to implement the Array.prototype.flat(), which reduces nesting of an array.

The boilerplate code:

function flat(arr, depth = 1) {
  // your implementation here
}

The flat method receives an array and a number to determine how many levels deep to flatten the array. If no number is provided, depth defaults to 1, because the flat method flattens by 1 by default.

An array called result, where flattened elements will be collected, is created

const result = [];

A helper function that takes 2 arguments is created to flatten the array recursively. The function loops through every index of the array. If there are indexes that don't exist, it skips them.

const flatten = (array, currentDepth) => {
    for (let i = 0; i < array.length; i++) {
      if (!(i in array)) {
        result.push();
        continue;
      }
}

The function runs recursively, checking each element of the array. If the element is another array, and the specified depth isn't reached, the function flattens it. If it's not an array or the depth is reached, it pushes the element into the results array.

const element = array[i];

      if (Array.isArray(element) && currentDepth < depth) {
        flatten(element, currentDepth + 1);
      } else {
        result.push(element);
      }

The results array is then returned. The final code looks like this:

function flat(arr, depth = 1) {
  // your implementation here
  const result = [];

  const flatten = (array, currentDepth) => {
    for (let i = 0; i < array.length; i++) {
      if (!(i in array)) {
        result.push();
        continue;
      }

      const element = array[i];

      if (Array.isArray(element) && currentDepth < depth) {
        flatten(element, currentDepth + 1);
      } else {
        result.push(element);
      }
    }
  };

  flatten(arr, 0);
  return result;
}

That's all folks!


This content originally appeared on DEV Community and was authored by Bukunmi Odugbesan


Print Share Comment Cite Upload Translate Updates
APA

Bukunmi Odugbesan | Sciencx (2025-10-07T22:48:54+00:00) Coding Challenge Practice – Question 23. Retrieved from https://www.scien.cx/2025/10/07/coding-challenge-practice-question-23/

MLA
" » Coding Challenge Practice – Question 23." Bukunmi Odugbesan | Sciencx - Tuesday October 7, 2025, https://www.scien.cx/2025/10/07/coding-challenge-practice-question-23/
HARVARD
Bukunmi Odugbesan | Sciencx Tuesday October 7, 2025 » Coding Challenge Practice – Question 23., viewed ,<https://www.scien.cx/2025/10/07/coding-challenge-practice-question-23/>
VANCOUVER
Bukunmi Odugbesan | Sciencx - » Coding Challenge Practice – Question 23. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/10/07/coding-challenge-practice-question-23/
CHICAGO
" » Coding Challenge Practice – Question 23." Bukunmi Odugbesan | Sciencx - Accessed . https://www.scien.cx/2025/10/07/coding-challenge-practice-question-23/
IEEE
" » Coding Challenge Practice – Question 23." Bukunmi Odugbesan | Sciencx [Online]. Available: https://www.scien.cx/2025/10/07/coding-challenge-practice-question-23/. [Accessed: ]
rf:citation
» Coding Challenge Practice – Question 23 | Bukunmi Odugbesan | Sciencx | https://www.scien.cx/2025/10/07/coding-challenge-practice-question-23/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.