steamrollerArray not returning array

Tell us what’s happening:
I wrote my function with several sanity checks to keep track of the array as it’s worked through each recursive step, and the final log of the array that should be returned appears to be showing the right answer. However, when I run the function in my console, the function returns undefined. I don’t understand why this is happening, especially since I am able to successfully log what appears to be the correct value in the line immediately before the return statement.

Your code so far

function steamrollArray(arr, safety = 0) {
  // I'm a steamroller, baby
  var callAgain = false;
  //console.log(arr, safety);
  if (safety > 100) {
    return("Safety exception");
  var arrOut = [];

  // For each element of the array
  arr.forEach(function(elem) {
    // If the element is an array
    if (Array.isArray(elem)) {
      // Flip switch to callAgain to true to call function again
      callAgain = true;
      // Push every sub-element of the array to arrOut
      elem.forEach(function(subElem) {
    } else {
  // If bad element caused callAgain to be flipped
  if (callAgain == true) {
    console.log("steamrolling again: ", "\narr: ", arr, "\narrOut: ", arrOut, '\n');
    // steamroll arrOut
    steamrollArray(arrOut, safety);
  } else {
    console.log("arrOut:", arrOut);
    return arrOut;

steamrollArray([1, [2], [3, [[4]]]]);

Your browser information:

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

Link to the challenge:

when you return from this, are you saving the result at all or doing anything with it? Otherwise, this recursive call has no purpose.

When I put a console.log('End of function') at the end of the function, I see that you are reaching the end of the function. If you reach the end of the function and return nothing, then your return is undefined. You need to think about your logic flow a little. What happens if it reaches the end of the file?

Also, in your if statement, you have steamrollArray(arrOut, safety); That function call is returning a value and you are not doing anything with it.

When I addressed those two problems, it worked.

1 Like