Create a Deep Flattening Tool - Create a Deep Flattening Tool

Tell us what’s happening:

Hello, I am struggling with this one. Initially I was struggling with having undefined values being pushed into my return array, but I realized it was because it had no return statement.
I updated that, and it started pushing actual values. However now I am having the issue of having the return statement in the for loop. Obviously, that will terminate after the first loop, so that won’t work. But I cannot for the life of me figure out how to hold the values from the for loop and push them afterwards.

I have tried adding an array outside of the for loop and pushing the values to that, but I run into the same issue of it pushing undefined values.

unwrapIfArr was initially made because of running into this same problem with steamrollArray. I thought creating a separate sub-method would fix the issue, but apparently I was wrong, lol.

It feels like there’s something simple I’m missing, but I can’t figure it out. Any help would be appreciated. Thank you!

Your code so far

function steamrollArray(nestArr) {
  function unwrapIfArr(element) {
    if (Array.isArray(element)) {
      console.log(`${element} is an array. Starting for loop`);
      for (let item of element) {
        console.log(`Current item:`);
        console.log(item);
        return unwrapIfArr(item);
      }
    }
    else {
      console.log(`${element} is not an array. Returning ${element}`);
      return element;
    }
  }

  let result = [];
  for (let item of nestArr) {
    let toPush = unwrapIfArr(item);
    console.log(`Item to push:`);
    console.log(toPush);
    result.push(toPush);
    console.log(`Pushed to result. New result:`);
    console.log(result);
  }

  console.log(`For loop completed. Returning result:`);
  console.log(result);
  return result;
}

steamrollArray([1, [2], [3, [[4]]]]);


Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36

Challenge Information:

Create a Deep Flattening Tool - Create a Deep Flattening Tool

you are doing lots of work with debugging, you are going well!

maybe you need an extra tool for debugging?

use a tool like https://pythontutor.com/, it helps visualize what the code does one line at a time

1 Like

I tested around a little bit…enough to see that you are not handling the second element in this array: [3, [[4]]]]. Maybe you could check the array length and use a while loop to address it? It looks like you’re using recursion…and this challenge is definitely suited for that type of approach. Matter of fact, if you use recursion and a default value for the return array in your function definition, you could streamline your function considerably.

1 Like

This helped me a lot. I realized I was greatly overcomplicating things. For starters, one of the tests said that I couldn’t use global variables, so I was focused on not using global variables. Upon reflection, I realized if I defined it with a global variable, I could just wrap everything in another function.

I got it working by having a recursive function push its argument to an array when it was not an array. If it was an array, it called itself once for each element.

Thank you for your help!