Steamroller- I don't know why it doesn't work

Tell us what’s happening:
Can someone explain what a I doing wrong? This is the first recursive function I ever made so I might be doing something worng. It works on my browser, it should also work here, but it doesn’t for some reason.

Your code so far


let finalArr = [];

function steamrollArray(arr) {
	for (let i of arr) {
		if (Array.isArray(i)) {
			steamrollArray(i);
		} else {
			finalArr.push(i);
		}
	}
	return finalArr;		
}

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

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.75 Safari/537.36.

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

@Zerox, welcome back to the forums,
While there is nothing wrong with your code per say and gives the correct answers, the tester does not like that finalArr is global, try to scope finalArr to inside steamrollArray() and it will pass.

1 Like