Steamroller | Recursion not working

Steamroller | Recursion not working
0.0 0

#1

My recursion is not working.
What am I doing wrong? :confused::confused::confused:

function steamrollArray(arr) {
  // I'm a steamroller, baby
  //return arr;
  var a =[];
  
  for (var i = 0; i < arr.length; i++){
    if (Array.isArray(arr[i])){
      for(var j = 0; j < arr[i].length; j++){
        if(Array.isArray(arr[i][j])){
          var item = steamrollArray(arr[i][j]);
          a.push(item);
        } else {
          a.push(arr[i][j]);
        }
      }
    } else {
      a.push(arr[i]);
    }
  }
  
  return a;

}

steamrollArray([1, {}, [3, [[4]]]]);

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36.

Link to the challenge:


#2

What do you mean by β€œnot working”? Are you getting errors? What is behaving differently than expected? What do you think should happen? What is happening instead?


#3

I want the code to keep on iterating on the items within the β€˜arr’ Array until every item within it is extracted out of the engraved arrays but it is only it is only iterating up to β€˜arr[i][j]’.

Example:

My target is
seamrollArray([1, {}, [3, [[4]]]]) should return [1, {}, 3, 4]

but it is returning
[1, {}, 3, [4]]

The last item [4] is still in an Array.

Similar case is happening with any array item which is engraved under 3 layers of arrays may be because the recursion part is not working (as i guess)

eg:

steamrollArray([[[β€œa”]], [[β€œb”]]]) should return [β€œa”, β€œb”]

[but it is returning [[β€œa”], [β€œb”]];

Hope I made the problem clear.
If yes then please advise any solution.

Thanks in advance


#4

Today you get to be the person Ariel helps just after that first precious hit of caffeine, which is a bit of a crapshoot.

I think that with lots and lots of log statements you will be able to see where the problem is coming from yourself.


#5

@ArielLeslie
Thank you very much, Ariel.

Apologies for the delay.
I was working on my Twitch API project. It’s also flooded with bugs. but I have managed to fix most of them.

So, After diving into your given code full of lots and lots of log statements. I found that my recursion in pushing that last engraved item, into an array and then it was again pushing it into my final returning array.

Congratulations to me, I solved it finally.
However, my solution seems mediocre.

Can you please review my code if you don’t mind.

function steamrollArray(arr) {

var a =[], item;
  
  for (var i = 0; i < arr.length; i++){
    if (Array.isArray(arr[i])){
      for(var j = 0; j < arr[i].length; j++){
        if(Array.isArray(arr[i][j])){
          item = steamrollArray(arr[i][j]);
          a.push(item[0]);
        } else {
          a.push(arr[i][j]);
        }
      }
    } else {
      a.push(arr[i]);
    }
  }
  
  return a;
}

Thanks again


#6

If you think about it, I bet that you can remove that nested for loop pretty easily.


#7

@ArielLeslie I will work on it today and I will try best to modify it.

Today I completed this Twitch project.

Could you please review it.

Thanks :grinning::grinning: