I'm having trouble with the steamroller challenge

Hoping somebody can help me here, I’m having a hard time trying to get my code to pass the tests for the steamroller challenge in the intermediate algorithm scripting section of the JS algorithms and data structures curriculum… The challenge is to flatten the given nested array, accounting for varying levels of nesting. I can get the function to return the correct result for the first call… here is what I have so far:

let result = [];
function steamrollArray(self) {
   
  for (let i of self) {

    if (Array.isArray(i) == true) {
      steamrollArray(i);

    }else {
      result.push(i);
    }
  }
  
  return result;
};

console.log(steamrollArray([1, {}, [3, [[4]]]]));
// console prints [ 1, {}, 3, 4 ] for above call

my issue is that I have declared the result variable outside of the function so on consecutive calls the result just gets added to. For some reason trying to return result.splice(0) returns an empty array. I have also tried to declare a new variable to match result and resetting the value of result before the return statement but I am not having any luck. any help would be greatly appreciated!

How about creating the function and moving result inside steamrollArray?

function steamrollArray(self) {
  const result = []

  function yourFunction(arr) {
    // your code
  }

  return result
}

Using a global variable to do faux-recursion is tempting, but it breaks when you run your function twice.

If you want to go with a recursive solution for this problem (I think it’s a good fit) then you should use your return value from the recursive call.

Here you are ignoring the return value, which means that you need to stash the result of the recursive call somewhere, but if you use the return value, then you don’t need to use a global variable to stash your work.

Hi @JeremyLT,

Thanks for your help, I was a bit confused on how to use the return value of the recursive calls, when I logged each call in the console each call showed the final resulting array ( in the above example I posted “[ 1, {}, 3, 4 ]”). After trying to refrain, I had a look at one of the solutions to help me understand this problem a little better. It seems my solution was very close heres how I got it to work properly:

function steamrollArray(self) {
  let result = []; 
  for (let i of self) {

    if (Array.isArray(i)) {
      //console.log(i, 'is array, flattening...')
      result.push(...steamrollArray(i));

    }else {
      //console.log(i, 'is not array, pushed to result array')
      result.push(i);
    }
  }
  
  return result;
};

I’m still not really sure I fully understand why this works the way it does:

result.push(...steamrollArray(i));

I’m just trying to wrap my head around why this doesn’t reset the result variable on each pass. :thinking:

Either way I appreciate you taking the time to have a look at my problem and nudge me in the right direction.

Each call to steamrollArray has its own result variable and its own scope, so this line

says, in words, “take the ‘result’ of calling steamrollArray on i and push it into the result array for the current function call”.

Side note - I think the variable names are perhaps a bit confusing:

function steamrollArray(array) {
  let flattenedArray = []; 

  for (let element of array) {
    if (Array.isArray(element)) {
      const flattenedElement = steamrollArray(element);
      flattenedArray.push(...flattenedElement));
    } else {
      flattenedArray.push(element);
    }
  }
  
  return flattenedArray;
};

Do these variable names help?

When you break it down like that it does make it a little easier to understand. What helped it click in my head was you declaring a ‘flattenedElement’ variable to show that its working on a result for the current call.

as for the variable names, I usually try to make things as mnemonic as possible, but with these challenges I can get lazy as it feels unnecessary until I run into a problem like this, and it just add an extra nested layer of confusion (bad pun intended).

Thanks again for all your help :smiley: Happy Coding!

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.