Intermediate Algorithm Scripting - Steamroller

The console log shows that this recursion is working, but the test results are all negative.

  **Your code so far**
function steamrollArray(arr) {
console.log(arr)
  if(!arr.some(Array.isArray)) {
    return arr;
  } else {
    let flatArray = [].concat(...arr);
    steamrollArray(flatArray);
  }
}
steamrollArray([1, {}, [3, [[4]]]]);
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36

Challenge: Intermediate Algorithm Scripting - Steamroller

Link to the challenge:

Your function always needs to return an array. I only see it returning an array in your base case.

So I guess you mean the terminating case is the base case and the recursive case also needs a return. It just seems odd to me to return the recursive call, but I did and it worked. Thx.

Don’t let the fact that it’s a recursive function call throw you. It’s still really just a regular function call, just like you would call any other function. Let’s take this trivial example:

function myRound(num) {
  return Math.round(num);
}

Do you have a problem understanding that num is passed into Math.round and then the value returned by Math.round is then returned by myRound? This is exactly what is happening with a recursive function call. The only difference is that you are calling the same function again. But you still need to return a value for the original function call.

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