Steamroller JavaScript not working so well

Tell us what’s happening:
Well, I made this monstrosity of a code. It console logs just right but I wont pass the test with it. Can someone give me a clue?

Your code so far

function steamrollArray(arr) {
  let babir = []
  for(let i = 0; i<arr.length;i++) {
    for(let j = 0; j <=100;j++) {
      if(arr[i].length == j ) {
  if (arr[0]==1) { babir.unshift(arr[0])}
  // I'm a steamroller, baby
 return babir;

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

console.log(steamrollArray([[["a"]], [["b"]]]))
console.log(steamrollArray([1, [2], [3, [[4]]]]))
console.log(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/75.0.3770.100 Safari/537.36.

Link to the challenge:

Sorry for that, it fails the “steamrollArray([1, {}, [3, [[4]]]])” test. Other than that its returning the right answers for the other tests, is it not?

The code you posted above fails all the tests.

The reason it fails steamrollArray([[[“a”]], [[“b”]]]) is because your function returns:

[ [ [ "a" ] ], [ [ "b" ] ] ]

instead of:

[ "a", "b" ]

In all the test case you are just recreating a new array with the same structure as the one passed to the function.

1 Like

The console here doesn’t show if object is an array or not, it doesn’t display square brackets… which I find quite confusing. For example the string “1,2,3,4” will look exactly the same as an array [1,2,3,4]…
I used to write some code in chrome console to see the real output (it shows if data is an array or not).
Some hints:

  1. Logical problem is that this is the thinking: take an element from an array, if it’s an array, take the first element of this array, if this is an array, take the first element… see where it goes? This is why the optimal solution will use some kind of a recursive function (one that calls itself)
  2. You need to create some kind of an accumulator array to which you will be adding items. Start with an empty array. Now write a function, that iterates through items of the provided array (the one that is passed as the argument, and not the accumulator one). If item is not an array, push it to accumulator. If it is an array, call the function again, but now iterate through item’s items. I hope it’s clear.
  3. You can iterate through array’s items using for loop or array.reduce method. Array.reduce in this case is my favourite, since it accepts callback (which may be recursive) and the callback arguments (first two) are: accumulator and current item.
    I hope I helped. I don’t want to post ready solutions, so I tried to describe it the best as I could

The browser console definitely shows it as an array. At this point in the curriculum, users have already learned the difference between the browser console and the “fake” FCC console.

This challenge is easily solved with recursion or an iterative solution.

1 Like

Thanks so much for your feedbacks! I went trough the problem again and solved it. It is good to see that I could find support here in the forum