Algorithm Scripting: Steamroller

Algorithm Scripting: Steamroller
0

#1

Tell us what’s happening:

I am missing something …somehow my code does not return the correct sequence.
the outputs are -
[“a”, “b”] for steamrollArray([[[“a”]], [[“b”]]])
[1, 2, 3] for steamrollArray([1, [2], [3, [[4]]]])
[1, undefined, 3] for steamrollArray([1, [], [3, [[4]]]])
[1, {…}, 3] for steamrollArray([1, {}, [3, [[4]]]])

Your code so far


function steamrollArray(arr) {
   function flatten(item){

     if(Array.isArray(item)){
       for (let a in item){
         return flatten(item[a]);
       }
     }else { return item;}}
     console.log(arr.map(flatten));
 return arr.map(flatten);
   }

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/68.0.3440.106 Safari/537.36.

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


#2

Try adding more console.log statements to help you understand the issues in the logic.

For eg, here’s what I did to your code: