Steamroller not recognizing correct output?

It looks like I’m getting the correct output (according to console.log call immediately before return statement), but still failing the tests. A picture says a thousand words: https://i.imgur.com/hRa3xHS.png

  **Your code so far**

function steamrollArray(arr) {
var done = true;
//assume this is the last iteration (all nested arrays are flattened)
for (let i = 0; i < arr.length; i++) {
  if (Array.isArray(arr[i])) {
    done = false;
    //check our assumption and change it if needed
  }
}
if (done) {
  console.log(arr);
  return arr;
//return the final array
} else {
  var newArr = arr.reduce((a, b) => a.concat(b), [])
  steamrollArray(newArr)
  //perform one "layer" of flattening and call the function again
}
}
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:86.0) Gecko/20100101 Firefox/86.0.

Challenge: Steamroller

Link to the challenge:

You might want to return this

Yeah. Literally caught that myself a minute after posting. Big oof.

Still curious why I can get the correct answer with “console.log(arr)” but not “return arr”? If the iteration is failing because i forgot to type return, why can I still console.log the correct answer?

You’re using tail recursion here, the whole problem is getting pushed deeper and deeper into the recursion until eventually everything is flat. But that solution never got lifted back up to the top level where the function was first called.

That makes sense. Thank you.

1 Like

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