Create a Deep Flattening Tool - Create a Deep Flattening Tool

Tell us what’s happening:

I managed to log each value on a single line, but when I tried using .push() to add each value into an empty array, the result wasn’t what I expected.

Your code so far

const steamrollArray = (sourceArr) => {
  let flattenedArr = [];

  const isArrayLengthOne = (Arr) => {
    if (Arr.length === 1) {
      return true;
    } else {
      return false;
    }
  };

  sourceArr.forEach((element) => {
    if (Array.isArray(element) === true) {
      // Yes, it is an array.
      if (isArrayLengthOne(element) === true) {
        // It's an array whose length is one.
        steamrollArray(element);
      } else {
        // It's an array whose length is not one.
        steamrollArray(element);
      }
    } else {
      // No, it is not an array.
      console.log(element, "No, it is not an array.");
      flattenedArr.push(element);
    }
  });

  return flattenedArr;
};

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:143.0) Gecko/20100101 Firefox/143.0

Challenge Information:

Create a Deep Flattening Tool - Create a Deep Flattening Tool
https://www.freecodecamp.org/learn/full-stack-developer/lab-deep-flattening-tool/create-a-deep-flattening-tool

Hi.

A few points on your code:

  1. Parameters are usually written with lower case (see where you have put “Arr”).
  2. These returns a boolean value - no need to add “=== true”.
Array.isArray(element)
isArrayLengthOne(element)
  1. You have an if and and else statement returning the same code. Why?
  2. You aren’t asked to return any text so remove the console statements.

What do you want your code to do if it is an array? I can’t see that your code is doing anything to an array.

Your code is pushing non arrays to your global variable. If you runt test 4, you can see this but as your code isn’t doing anything with arrays it needs more work.

With this one I suggest you write down what you need your code to do and then build each part.

I realised I made a mistake by writing the name in capitals.

To make things clearer to me as a newbie, I drafted the code so I could properly see the outcome.

My original idea was to use recursion to pick out the first element in an array that meets the requirement.

Yes you need to update your code to do something to the array.

I think you also need to address what to do if the array length is more than 1 like some of the later tests.

The lab “Create a Deep Flattening Tool” is placed in the “Higher Order Functions and Callbacks” section.

However, the “Recursion” section comes after that in the curriculum.

So I’m not completely sure if I’ve learned enough concepts yet to complete this lab confidently.

There are ways of doing it using the skills learned up to this point including those in this chapter. It might be a good idea to have a look online and focus on methods you have learned so far.

I looked up a few resources online — both on MDN and freeCodeCamp — and found that each of them explains a different approach to this lab:

At the moment, the .forEach() method makes the most sense to me.
It’s quite similar to what I first had in mind.

I still can’t come up with an approach like the one shown on freeCodeCamp, though — even though it only uses array indexes.

I didn’t even think of a non-recursive approach when I did this one: My brain is presently having trouble even imagining how that would work. It’s still Oktoberfest in Munich, I hear; maybe a couple of Maßkrüge would help.

2 Likes

I’ve seen the reduce method used as well.

1 Like

The code you posted is actually on its way to becoming a workable solution. Taking into consideration the things that @a1legalfreelance pointed out, also think about why you even need to think about the array length if you’re going to call the function recursively until it’s no longer an array. So, that bit can go.

Then when you do call the function recursively, what parameter would you need to pass in to retain the nested values? What can you add to your array definition to accommodate that?

Of course, you can still tackle a non-recursive approach, but it’s a bit more painful. :slight_smile:

2 Likes

MDN also introduces an approach to flattening a nested array that’s worth noting for future reference.

Thanks for that. I’m revisiting these as some of these as the later projects involve flattening an array.

1 Like

I recently refactored a recursive array-flattening function, replacing .forEach() with .reduce() to make the code more functional and concise. The updated version works perfectly and is easier to read — a small but meaningful improvement!

3 Likes