I have tested my code using console.log(arr) for the else function (Function 1) and it return the elements of the array, but I can not .push them into my result array (Function 2). I have used typeof(arr) in Function 3 to see whether the function returns numbers and strings which is does, so I am not sure, why I can not push the arr into result. Any help is appreciated!
Function 1
function steamrollArray(arr) {
var result = [];
if (Array.isArray(arr) === true) {
for (const key in arr) {
steamrollArray(arr[key]);
}
} else {
console.log(arr);
}
}
steamrollArray([1, [2], [3, [[4]]]]);
Function 2
function steamrollArray(arr) {
var result = [];
if (Array.isArray(arr) === true) {
for (const key in arr) {
steamrollArray(arr[key]);
}
} else {
result.push(arr);
}
return result;
}
steamrollArray([1, [2], [3, [[4]]]]);
Function 3
function steamrollArray(arr) {
var result = [];
if (Array.isArray(arr) === true) {
for (const key in arr) {
steamrollArray(arr[key]);
}
} else {
console.log(typeof(arr);
}
}
steamrollArray([1, [2], [3, [[4]]]]);
Your browser information:
User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:86.0) Gecko/20100101 Firefox/86.0
.
Challenge: Steamroller
Link to the challenge: