Help: intermediate challenge - SteamRoller

Im really tired of looking at my code - I tried checking out the guide for this challenge as well

https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/steamroller

I tried for an hour to get a working solution without recursion, because i dont understand it, then i spent the next hour trying to understand the solutions in the guides

Whats a good way to break down this challenge and when should we use recursion?

function steamrollArray(arr) {
	let newArr = [];
	let reduction = function(arg) {
		if (!Array.isArray(arg)) {
			newArr.push(arg);
		} else {
			reduction(arg)
		};
		console.log(newArr);
	}
	reduction(arr);
}; //function
steamrollArray([1, [2],
	[3, [
		[4]
	]]
]);

you are kinda just going in a loop passing arr and then arg to the function without ever changing anything

do you ever want to check the array elements?

Yeah i would like to solve this challenge, how would you attempt it yourself?

This was my most original attempt BTW

function steamrollArray(arr) {
	let newArr = [];
	for (let i = 0; i < arr.length; i++) {
		console.log("loop, ", i);
		if (typeof arr[i] === "string" || typeof arr[i] === "number") {
			newArr.push(arr[i]);
		}
		if (typeof arr[i] === "object") {
			console.log("we got an object here");
			console.log(arr[i]);
			if (Array.isArray(arr[i])) {
				console.log("we got an ARRAY here");
				console.log(arr[i]);
			};
		}
	} //loop
	console.log("SOLUTION: ", newArr);
} //function

you know that arr is an array, but you pass it directly to reduction to check if it is an array, well, you already know it is - maybe you want to pass to reduction the array elements

If you’re trying to do this manually, try using a while loop with a .some((arrayElement) => arrayElement instanceof Array) check on the array and evacuate elements from nested arrays gradually till the check in the while is false.

However, that’s if you’re trying to do it manually. Are you aware that there’s a built-in function for this purpose?

If you’re not aware, check .flat() on https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat