Correct output but not passing tests!

Tell us what’s happening:
Hello! I’m not sure what I’m doing wrong here, but recursive functions are also super confusing. Help please?

Your code so far


function steamrollArray(arr) {
// I'm a steamroller, baby
const steam = function(arr, result = []){
for (let i = 0, length = arr.length; i < length; i++){
  const value = arr[i];
  if (Array.isArray(value)){
    steam(value, result);
  } else {
    result.push(value);
  }
}
return result;
};
console.log(steam(arr,[]));
}

steamrollArray([1, [2], [3, [[4]]]]);
steamrollArray([[["a"]], [["b"]]]);
steamrollArray([1, [], [3, [[4]]]]);
steamrollArray([1, {}, [3, [[4]]]]);
steamrollArray([1, [2], {}, [3, [[4,[5]]]]]);

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36.

Challenge: Steamroller

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/steamroller

your function steamrollArray returns undefined
why? well, you don’t have a return statement inside that function (a console.log is not a return statement! one prints to the console, the other define the function output)

also, length is never defined, that gives an error in strict mode, and stop the function

1 Like

The thing is you declare steam function but you never invoke it, passing the arr argument. Also I think it would be much easier to use an array outside steam function and from within inside steam push items into it when it s the case or if you want to keep the code as it is, since you need to invoke steam you can assign the function invocation to a constant which will hold the result and return that constant.

1 Like

the return statements always get me! thank you so much for your help!

assigning the function invocation to a constant was a great idea! worked like a charm! thank you so much for your help!