Create a Deep Flattening Tool - Create a Deep Flattening Tool

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

Not sure this is the best way to do this. That said, what happens here?

It seems like this line is not successful? Why don’t you log arrAsString to the console and see if the replace worked?

What variables are you talking about?

1 Like

I went ahead and added a console statement to log the string before any further code is ran in the program.

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]", "{}");

  }




  // See if the replacement worked

  console.log("String without '[object Object]': \n" + arrAsString);

  console.log("\n");




  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)); */

Console Output:

Array as string: 1[object Object]3,4
Variable Type: string


Checkpoint Reached

String without '[object Object]': 
1[object Object]3,4


Flattened Array:
[ 1,
  '[',
  'o',
  'b',
  'j',
  'e',
  'c',
  't',
  0,
  'O',
  'b',
  'j',
  'e',
  'c',
  't',
  ']',
  3,
  4 ]

As for the variables I am referring to, those would be the following:

const arrAsString = nestedArr.join("");

const newArr = [];

let tempVal;

The thing is these are not global variables, as they are nested within a function. Unless I have a misunderstanding of the test case.

The following test case fails since it says I must not use global variables:
7. You should not use global variables.

focus on making your function work for any input (and pass test 5)

then also test 7 will work

1 Like

Why isn’t replace working? Are you using it correctly?

Even if you get it to work though, you are hard coding a specific case. This will likely fail a more general test.

Yes, they are not global variables.

This test might not be passing because you are hard coding a specific case.

1 Like

Did you read the entire thread and visit the recommended links? I did not see any global variables being used. I understand that you probably passed on recursion because it hasn’t been introduced in the curriculum yet, but there were several other good suggestions there.

At any rate, converting the array to a string for this challenge is not a good idea. I suggest you take another look at that thread and consider another approach.

1 Like

Ah, I went ahead and sifted through the links in the thread. I came across one link that talked about a recursive approach to flattening arrays on a freeCodeCamp article as well as one solution that seemed to be working for the person that posted it.

However, when I employ this approach it doesn’t seem to be functional for this lab. It is saying that the parameter being received, nestedArr, is not being used for some reason. I added various comments to the code to sort of decipher what each line of code means rather than just straight up copying it and hoping it works.

function steamrollArray(nestedArr) {

  // Function expression assigning its result to flatten

  const flatten = (nestedArr) => {

    // Acculumator which is initialized to an empty array and the current element

    return nestedArr.reduce((acc, cur) => {

      // Check if the element is an array, if it is push the rest.

      if (Array.isArray(cur)) {

        acc.push(...flatten(cur));

        return acc;

        // Just push one element

      } else {

        acc.push(cur);

        return acc;

      }

      // Initial value for the accumulator (acc) set to an empty array [] which will store each value.

    }, []);

  };

}




console.log("Refined Array:");

console.log(steamrollArray([1, {}, [3, [[4]]]]));



Console Output:

Refined Array:
undefined

You have a function within a function, so no, steamrollArray is not using that parameter.

1 Like

you are never calling flatten and steamrollArray does not have a return

1 Like

Ah, I see. I don’t know how to refactor this recursive solution to fit my lab. I went back to the freeCodeCamp article that covered flattening arrays using a recursive approach and utilized that technique. I added the helper function seen in the article which allowed me to apply it to this lab and I managed to pass.

However, I still don’t understand how this approach is dealing with the element that is an array when it encounters it. I can post the article link, but it does have the solution.

then you should see in there how the helper function is used, you need to use it after creating it, or the approach is not going to work

like, you do not expect steamrollArray to run without calling it, right? this is what you are missing with flatten

1 Like