Tell us what’s happening:
I am having issues with this lab pertaining to the use of global variables and the object nested within the array being improperly printed out in the new array.
Your code so far
function steamrollArray(nestedArr) {
const arrAsString = nestedArr.join("");
const newArr = [];
let tempVal;
console.log("Array as string: " + arrAsString);
console.log("Variable Type: " + typeof arrAsString);
console.log("\n");
if (arrAsString.includes("[object Object]")) {
// Program reaches this checkpoint
console.log("Checkpoint Reached\n");
arrAsString.replace("[object Object]", "{}");
}
for (const element of arrAsString) {
if (element !== ",") {
if (isNaN(element * 2) === false) {
tempVal = Number(element);
newArr.push(tempVal);
} else {
newArr.push(element);
}
}
}
console.log("Flattened Array:");
return newArr;
}
console.log(steamrollArray([1, {}, [3, [[4]]]]));
/* let string = steamrollArray([1, [2], [3, [[4]]]]);
console.log(JSON.stringify(string)); */
I went ahead and used to “Get a Hint” feature, but the solution the person had made back in October of 2025 utilized a recursive approach and had a global variable. So, with that in mind I am left rather confused on how I can proceed without the use of global variables. Furthermore, I added some console commands within the code to see if the program reaches that statement within the 1st if branch.
Quick Edit: Wouldn’t these variables be considered to be within a function scope? These are not accessible from anywhere in the program. Only in the function itself, so maybe I am misinterpreting this test case.
Console Output:
Array as string: 1[object Object]3,4
Variable Type: string
Checkpoint Reached
Flattened Array:
[ 1,
'[',
'o',
'b',
'j',
'e',
'c',
't',
0,
'O',
'b',
'j',
'e',
'c',
't',
']',
3,
4 ]
Failed Test Cases:
5. steamrollArray([1, {}, [3, [[4]]]]) should return [1, {}, 3, 4].
7. You should not use global variables.
I managed to pass every other test case.
Challenge Information:
Create a Deep Flattening Tool - Create a Deep Flattening Tool