# Intermediate Algorithm Scripting: Steamroller

My output matches what the test is expecting, but it fails all of the tests.

Here is my output from the console. All output matches the listed expected output from the unit tests.

[“a”, “b”]
[1, 2, 3, 4]
[1, 3, 4]
[1, {…}, 3, 4]

Here is my code:

``````function steamrollArray(arr) {
// I'm a steamroller, baby

var bool = false; //track if an item is an array
var newArray = []; //empty array to use with concat

newArray = newArray.concat(...arr);//flatten the array

//loop through the array and flag if an item is a nested array
for (let i = 0; i < newArray.length; i++) {
if (Array.isArray(newArray[i])) {
bool = true;
}
}

//if bool = true for a value, recurse through function
if (bool) {
steamrollArray(newArray);
} else { //otherwise return the value of newArray
console.log(newArray);
return newArray;
}

}

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

You aren’t actually returning your final solution. Test it here.

1 Like

It looks like I was missing the return in the recursive function call. Changed it to the following and it works. I’m new to recursion, now I have to wrap my head around why I needed a return for the function call. Any help in explaining the details there would be much appreciated.

``````function steamrollArray(arr) {
// I'm a steamroller, baby

var bool = false; //track if an item is an array
var newArray = []; //empty array to use with concat

newArray = newArray.concat(...arr);//flatten the array

//loop through the array and flag if an item is a nested array
for (let i = 0; i < newArray.length; i++) {
if (Array.isArray(newArray[i])) {
bool = true;
}
}

//if bool = true for a value, recurse through function
if (bool) {
return steamrollArray(newArray);
} else { //otherwise return the value of newArray
return newArray;
}

}

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

I think I might get it now. Without the return it was just calling the function one time after the if statement when the value was true. That function call happened to result in the else code executing which is why I saw the correct values in the console, but the main function was not returning anything, just the single call to the function after the if was returning a value.

Is that correct?

``````function steamrollArray(arr) {
// I'm a steamroller, baby
for(var i=0;i<arr.length;i++){
if(arr[i].constructor==Array){
arr=arr.concat(arr[i]);
arr.splice(i,1)
i=-1;
}
}
return arr;
}
steamrollArray([[["a"]], [["b"]]])
``````

Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, if you post a full passing solution to a challenge and have questions about it, please surround it with `[spoiler]` and `[/spoiler]` tags on the line above and below your solution code.

Thank you.

You don’t need no fancy stuff to do this.

1. Just turn entire arr into string with String(arr).
2. Then you need to split it into array again using split() method where as parameter you put comma sign.
3. After that, you’ll need to convert array members back to numbers by using Number() method on each of them. Simple for loop or es6 map will do. My vote is for-loop since you’re a beginner.

Spoiler one-liner:

``````function steamrollArray(arr) {
// I'm a steamroller, baby
var res1 = String(arr).split(",").map(x => Number(x));

return res1;
}

console.log(steamrollArray([1, [2], [3, [[4]]]]));

``````

I am closing this thread, because it appears to have turned into a “Here’s my solution” without helping to solve the OP’s code problems.

3 Likes