Recursion is giving unexpected result on the JS streamroller challenge (flattening nested arrays)

I tried to work the JavaScript challenge of - flattening nested arrays - using recursion but I keep getting “[1]” as the return value, please help me find what I am (obviously) not getting right

Here’s my code:


function steamrollArray(arr) {
const flattened = [];
for (let i = 0; i < arr.length; i++) {
  if (Array.isArray(arr[i])) {
    flattened.concat(steamrollArray(arr[i]));
  } else {
    flattened.push(arr[i]);
  }
  return flattened;
}
}

console.log(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/88.0.4324.96 Safari/537.36.

Challenge: Steamroller

Link to the challenge:

Currently checking for the entire answer, but here is the first bit:
The “return flattened” should be outside of the for loop.

Ok I got it. The problem is, that whenever you have a nested array you rerun the function, which is kind of the principle of recursion BUT. every time you rerun the function, you also reset the value of flattened. So in order to avoid that, you can simply declare another empty solution array outside of the function and push the values you want to that.

In order to figure this out the following helped:

  1. use console.log within your function to see up until which point everything is working fine. (for example the fact, that i only reaches the value 0 pointed me to the fact, that you had a return within the for loop)

  2. since you know what you mean by writing the code it can be tricky to understand why the computer still doesn’t do what you want, since everything looks right.

In these cases I recommend just ignoring everything you think and go through your logic step by step with an example.

In this case it is the first time that you encounter a nested array and rerun the function, where it fails.

Hope this helped :slight_smile:

1 Like

Yeah, your return needs to be outside of your loop.

You should not use a global variable. Global variables will cause problems with the tests and global variables are generally best avoided.

I would instead investigate your concat. It is not behaving as you expect.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat

2 Likes

Thanks to @michaelmanke00 and @JeremyLT , I was able to develop a solution after reading their replies. The return statement was wrongly placed in the loop like Mike pointed out and also I wasn’t using the concat method right as was stated by Jeremy.

Here’s is the updated code, it woks.

function steamrollArray(arr) {
  let flattened = [];
  for (let i = 0; i < arr.length; i++) {
    if (Array.isArray(arr[i])) {
      flattened = flattened.concat(steamrollArray(arr[i]));
    } else {
      flattened.push(arr[i]);
    }
  }
  return flattened;
}

console.log(steamrollArray([1, [2], [3, [[4]]]]));
1 Like

Nice work getting it to run!