just before returning the value in else
block, the value is correct but after it returns, return value changes to undefined
My code so far
function steamrollArray(arr) {
let newArr = [];
let isNested = false;
for (let i=0; i<=arr.length-1; i++) {
if (arr[i].length == undefined || typeof arr[i] === "string" ) {
newArr.push(arr[i])
} else {
if (arr[i].length > 1) {
for (let j=0; j<=arr[i].length-1; j++) {
if (arr[i][j].length != undefined) {
isNested = true;
}
newArr.push(arr[i][j])
}
} else {
if (arr[i][0].length != undefined) {
isNested = true;
}
newArr.push(arr[i][0])
}
}
}
if (isNested) {
steamrollArray(newArr)
} else {
console.log("return Value inside else block: ", newArr)
return newArr;
}
}
console.log("function output: ", steamrollArray([1, [2], [3, [[4]]]]));
Output of function
return Value inside else block: [ 1, 2, 3, 4 ]
function output: undefined
Your browser information:
User Agent is: Mozilla/5.0 (X11; Linux x86_64; rv:109.0) Gecko/20100101 Firefox/116.0
Challenge: Intermediate Algorithm Scripting - Steamroller
Link to the challenge: