Steamroller steamroller

Steamroller steamroller
0

#1

hi campers , I got stuck in this challenge
he is my try

function steamrollArray(arr) {
  // I'm a steamroller, baby
let flat=[];
  for (let i=0;i<arr.length;i++){
      var element=arr[i];
      if(typeof arr[i]==="number"){
          flat.push(arr[i]);
      }
 
      else if(Array.isArray(arr[i])){
          
         for (let j=0;j<arr[i].length;j++){
             element=element[j];
             if(typeof element==="number"){
                 flat.push(element);
             }
             else {
                 j=j-1; //I am trying to stay in the same index to get a number .
                 
             }
         }   
      }     
  }
    return console.log(flat)
  
}

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

give a sign please ,and is there a proper way to stay in the same index when we use loop.
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/steamroller/


#2

Please post the link to the challenge now and in the future. It would make helping you more convenient.

So far, your steamroll doesn’t take into account an array inside an array.


#3
function steamrollArray(arr) {
     return arr.reduce((accumulator, currentValue) => {
        return accumulator.concat(currentValue);
    }, []).reduce((acc, cumm) => {
        return acc.concat(cumm)
    }, []).reduce((acc1, cumm1) => {
        return acc1.concat(cumm1);
    }, []).reduce((acc2, cumm2) => {
        return acc2.concat(cumm2);
    }, []);
}
steamrollArray([1, [2], [3, [[4]]]]);

#4

That may work for the test cases, but can you make it work in any case? For example if you have a deeper array than that.


#5

I hope it will help you


function checkRoller(arr) {
    return arr.reduce((accumulator, currentValue) => {
        return accumulator.concat(currentValue);
    }, []);
}
function checkMultiDimensionalArray(arr) {
    let i = 0;
    for (i; i < arr.length; i++) {
        if (Array.isArray(arr[i])) {
            return true;
        }
    }
    return false;
}
let rollerArray = [];
function steamrollArray(arr) {
    rollerArray = checkRoller(arr);
    if (checkMultiDimensionalArray(rollerArray)) {
        steamrollArray(rollerArray);
    }
    if (!checkMultiDimensionalArray(rollerArray)) {
        return rollerArray;
    }
}

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

#6

Awesome, now can you please format your code surrounding it with backticks? You can select your code and press the </> button in the post editor