# freeCodeCamp Challenge Guide: Steamroller

``````var result;
function steamrollArray(arr) {
result = [];
flatten(arr);
return result;
}

function flatten(arr){
if(!Array.isArray(arr)){
result.push(arr);
} else {
for(var i=0; i<arr.length; i++){
flatten(arr[i]);
}
}
}``````
1 Like

Here is my recursive solution:

``````function checkArray(arr, results){
for(var x = 0; x < arr.length; x++){
if(!Array.isArray(arr[x])) results.push(arr[x]);
else checkArr(arr[x], results);
}
return results;
}

function steamrollArray(arr) {
return checkArray(arguments, []);
}
``````

OR

you could always iterate:

``````function steamrollArray(arr){
var newArr = [];

for(var a = 0; a < arguments.length; a++){
if(Array.isArray(arguments[a])){
for(var b = 0; b < arguments[a].length; b++){
if(Array.isArray(arguments[a][b])){
for(var c = 0; c < arguments[a][b].length; c++){
if(Array.isArray(arguments[a][b][c])){
for(var d = 0; d < arguments[a][b][c].length; d++){
if(Array.isArray(arguments[a][b][c][d])){
newArr.push(arguments[a][b][c][d][0]);
} else { newArr.push(arguments[a][b][c][d]); }
}
} else { newArr.push(arguments[a][b][c]); }
}
} else { newArr.push(arguments[a][b]); }
}
} else { newArr.push(arguments[a]); }
}
return newArr;
}``````
2 Likes

### Use Recursion

``````
function steamrollArray(arr) {
// I'm a steamroller, baby
var newArr = [];

for (e of arr) {
if (Array.isArray(e))
newArr = newArr.concat(steamrollArray(e));
else
newArr.push(e);
}
return newArr;
}

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

``````
2 Likes

My solution :
function steamrollArray(arr) {
// Iām a steamroller, baby
var newArr = [];
function recur(el) {
if (Array.isArray(el)) {
el.map(function(xl) {
recur(xl);
});
} else {
newArr.push(el);
}
}

recur(arr);
return newArr;
}

I like your code sir. Simplest most intuitive iāve found so far. Thank you for sharing!

1 Like

Hi There,

I solved this code as below:
/* āesversion:6ā */
let newArr = [];
function steamrollArray(arr) {
// Iām a steamroller, baby

for(let i =0; i< arr.length; i++){
if(Array.isArray(arr[i])){
steamrollArray(arr[i]);
}

``````else newArr.push(arr[i]);
``````

}

return newArr;
}

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

Why is it not showing correct when I get all results as expected? I checked with all samples

Thank you for appreciating. Means a lot to me.

Need help! My program (given below) passes all test cases when I run them, but it is not getting verified:

``````var ret = [];

function steamrollArray(arr) {
// I'm a steamroller, baby
for(var i in arr) {
if( Array.isArray(arr[i]) ) {
steamrollArray(arr[i]);
} else {
ret.push(arr[i]);
}
}

return ret;
}

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

Output:
[1,{},3,4]

Same problem hereā¦ freeCodeCamp Algorithm Challenge Guide: Steamroller

Took some time to make this work!

feedbacks are welcome

function steamrollArray(arr) {

var aFlat = [];

flattenData(arr,aFlat);

return aFlat;
}

function flattenData(aOrig, aFlatten){

while(aOrig.length){
var oNext = aOrig.shift();

``````if (Array.isArray(oNext)){
flattenData(oNext,aFlatten);
}
else{
aFlatten.push(oNext);
}
``````

}
}

This one messed with my brain for some reason and I ended up solving it in a kind of wacky way:

``````function steamrollArray(arr) {

for (var x in arr) {
if (!Array.isArray(arr[x])) {
arr[x] = [arr[x]];
}
}

arr = arr.reduce(function(a, b) {
return a.concat(b);
});

for (var y in arr) {
if (Array.isArray(arr[y])) {
arr[y] = steamrollArray(arr[y]);
if (Array.isArray(arr[y])) {
arr[y] = arr[y][0];
}
}
}

return arr;

}``````

I donāt know why it didnāt occur to me to just build a new array with the recursion rather than reassemble it in place like I did here.

``````// jshint esversion:6
function steamrollArray(arr) {
let flat = [];

function flatten(n) {
for (let i = 0; i < arr.length; i++) {
Array.isArray(n[i]) ? flatten(n[i]) : flat.push(n[i]);
}
}

flatten(arr);
return flat;
}
``````

My code works and passes the test, but the linter is showing an error on the line with the ternary operator, āExpected an assignment or function call and instead saw an expression.ā Can I ignore this error/warning, or should i change my code?
Also, is it acceptable to use recursion inside of a loop like this?

If not Iāve got this solution without the loop, but with the same linter error:

``````// jshint esversion:6
function steamrollArray(arr) {
let flat = [];

function flatten(n) {
Array.isArray(n) ? n.forEach(flatten) : flat.push(n);
}

arr.forEach(flatten);
return flat;
}
``````

Thanks everyone!

The advanced code solution is advanced because it does not use recursion or because it uses things like transforming an array into a string?

Having issue here, I am getting the correct answers on the left when i test, but itās saying theyāre all wrong. my code is:

``````var totalRes = [];
function flattenArray(arr) {
for(var i = 0;i<arr.length;i++) {
if(Array.isArray(arr[i])) {
flattenArray(arr[i]);
}
else {
totalRes = totalRes.concat(arr[i]);
}
}
}

function steamrollArray(arr) {
return flattenArray(arr);
}

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

Here is another recursive solution
function steamrollArray(arr) {
// Iām a steamroller, baby

``````if(checkForArray(arr)){
//reduce() function is very useful in unnesting nested arraies
arr = arr.reduce(function(a,b){
return a.concat(b);
},[]);
//here we use recursion to recheck if array still has a nested array
if(checkForArray(arr))
return steamrollArray(arr);
}
``````

return arr;

}
//method to check if array still has a nested array
function checkForArray(arr){
for (var i = 0; i < arr.length;i++){
if (Array.isArray(arr[i])){
return true;
}
}
return false;

}

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

Iām not understanding the solution.

How is this:

``````for (var a in arg) {
flatten(arg[a]);
}
``````

getting deeper into the array? I get that itās running the code but how does arg[a] change to become arg[a][a]?

1 Like

This was my solution. I didnāt use recursion - Iām not very good with recursion yet.

``````function steamrollArray(arr) {

for(var i = 0; i <=arr.length; i++) {
arr = arr.reduce(function(a, b) {
return a.concat(b);
},[]);
}
return arr;
}

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

Hello all !

My first run at this challenge looked like this:

``````    function steamrollArray(arr) {
// I'm a steamroller, baby
var flatArr;
function hydraPress (a, b) { return a.concat(b); }
flatArr = arr.reduce(hydraPress,[]).reduce(hydraPress,[]).reduce(hydraPress,[]);
return arr;
}
//There is no spoon!
steamrollArray([1, [2], [3, [[4]]]]);
``````

Then started to think this solution was not very flexible and will not work in to many cases, even if it solved the 4 points from the challenge.
The second run, I worked on another approach that I think it will be more flexible and cover more possibilities. Here it is:

``````    function steamrollArray(arr) {
// I'm a steamroller, baby
function hydraPress (a, b) { return a.concat(b); }

for (var i = 0; i <= arr.length; i++) {
if(Array.isArray(arr[i])) {
arr = arr.reduce(hydraPress,[]); i--;
}
}
return arr;
}
//Don't hurt 'em, Hammer!
steamrollArray([1, [2], [3, [[4]]]]);
``````
1 Like

I use the regex approach, but with some differences, Surprisingly, this way performs very well:

``````    function steamrollArray(arr) {
// 1
var tmp =  JSON.stringify(arr);
//2
tmp = tmp.match(/([^\[\],\s]+)/g);
//3
tmp = JSON.parse( "["+tmp.join(',')+"]" );
return tmp;
}
``````
1. convert the array to string using the stringify method
2. catch everything that is not a bracket, semicolon or space
3. join the resulting array into a string and wrap it in brakets then convert it to array
11 Likes