DEV Community

Cover image for ArrayCeption
Kenny
Kenny

Posted on

ArrayCeption

Hello Everyone!
We're going to work on the toy problem called ArrayCeption! Dealing with an array with an array with an array of arbitrarily nested arrays and have it return the deepest level that contains a non-array value. Essentially we're just getting the deepest level of an array with nested arrays.
Alt Text

To start lets break the problem down a little bit!

  • Input: Array

  • Output: A Number of how deep the array is
  • Since the output is a number we can a counter system for if every array we traverse into is an array we can increase that counter.

    const getDeepestLevel = (array) => {
      // This function wants to know how deep an array with an arbitrarily nested arrays
      // Input: an Array
      // Output: a number that tells us how deep the array is
      // Lets create a counter variable to keep track of how deep the nested array is
      let counter = 0;
    };
    

    Now that we have our counter, next we can create an recursive function that'll do the work for us.
    This function will take the array and the depth of the array.
    What this function should do is to iterate through the array and check if there are any array within the array and iterate through that array that while increasing the depth level.
    Once we've hit to the point where the iterated array is no longer an array we'll set the counter variable that we have in the outer scope to the to the amount of times we've iterated through that array.

      function check(array, level){
        // Iterate through the array given
        array.forEach(element => {
          // check if the element being iterated is an array
            if (Array.isArray(element)){
            // recurse the element with and increase the level by 1
            check(element, level + 1);
            // once the iterated array is no longer an array
          } else if (!Array.isArray(element)) {
            // initialize the counter as the level where it last stop its iteration
            counter = Math.max(counter, level + 1);
          } else {
            return;
          }
        });
      }
    

    Once we've finished making our function we can just simply call the function we've created with array and the depth level.
    Since we want to know the initial depth we can just start at 0

    Our final function solution should look something similar to this:

    const getDeepestLevel = (array) => {
      // This function wants to know how deep an array with an arbitrarily nested arrays
      // Input: an Array
      // Output: a number that tells us how deep the array is
      // Lets create a counter variable to keep track of how deep the nested array is
      let counter = 0;
      // create a recursive function that'll take the array and set a depth level
      function check(array, level){
        // Iterate through the array given
        array.forEach(element => {
          // check if the element being iterated is an array
            if (Array.isArray(element)){
            // recurse the element with and increase the level by 1
            check(element, level + 1);
            // once the iterated array is no longer an array
          } else if (!Array.isArray(element)) {
            // initialize the counter as the level where it last stop its iteration
            counter = Math.max(counter, level + 1);
          } else {
            return;
          }
        });
      }
      // recall the function with the given array with the depth level of 0
      check(array, 0);
      // return the counter
      return counter;
    };
    

    Thank you for taking the time to view over this post, I hope this helps for those who had troubles with this toy problem.
    Til next time!

    Top comments (0)