Intermediate Algorithm Scripting - Steamroller

Tell us what’s happening:
hey guys.
how can I solve this? I want my program to keep making loops until all elements are not arrays.
at first I loop original array, it takes out the first element which is not an array.
then I want the program to keep looping and be doing the same thing.
thoughts?
Thank you.
P.S. How can I access those extractedelements? I have added another code.
so if those elements are arrays how do I access them?

  **Your code so far**
function steamrollArray(arr) {
const newArr = [];
let extractedElements;
for (let i = 0; i < arr.length; i++) {
  if (!Array.isArray(arr[i])) {
    extractedElements = arr.shift();
    newArr.push(extractedElements);
  }
  while (Array.isArray(arr[i])) {
if (Array.isArray(extractedElements)) {
  
}
  }
}
}



steamrollArray([1, [2], [3, [[4]]]]);
/*
loop arr, push any elements that are not an array to the newArr;
if element is an array, loop again until not array. and then push the non array element to newArr;
*/
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/104.0.0.0 Safari/537.36

Challenge: Intermediate Algorithm Scripting - Steamroller

Link to the challenge:

I was told that this challenge is a great opportunity to apply your knowledge of recursion. If I am not mistaken, @JeremyLT told me this when I asked some stuff about recursion.

As far as I remember, every solution from guide includes some usage of recursion.

I also used recursion to solve this.

Recursion is not needed. There is always an iterative solution. The recursive solutions are a bit more concise though.

As a rule of thumb, I use a for loop if there is a fixed number of iterations (like traversing an array), and a while loop or recursion for an unknown number of iterations (like repeating cde until a condition is met).

This is one of the handful of challenges in the curriculum where recursion is actually a good fit, though you certainly can do the challenge without recursion as well.

Hello.
I am trying to solve it again.

function steamrollArray(arr) {
  // loop array, if element isn't an array, push to new array.
  // if elements are array keep looping until is not.
  let newArr = [];
  for (let i = 0; i < arr.length; i++) {
    if (!Array.isArray(arr[i])) {
      newArr.push(arr[i]);
    }
    else {

    }
  }

}

steamrollArray([1, [2], [3, [[4]]]]);

My question is on my else statement I want to recurse (if I have typed correctly) the function to keep looping the array, I am not sure how to type it. Would you be able to clarify it for me?
Ty.

The big question is what should you do if you have a subarray? That subarray might need flattening, right? What would you want to do with a flattened subarray?

Here is the solution:

Mod Edit: SOLUTION REMOVED

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

so I loop → if i reach sub array keep looping until is not.
→ push elements that are not arrays to my newArr.

I’m not sure what ‘keep looping until not’ means?

Keep looping until elements are no longer arrays.

I’m not quite sure what you plan to do inside of the loops to remove the sub-arrays

lets use this example
I looped once, and first element isn’t an array so I push it to my array, I want the loop to continue on and on until an element isn’t an array → push and so on.

But you aren’t doing anything to the sub-arrays, so you will keep going forever…

You don’t want to change the order of the elements.

In my solution, it seems like I dont need to. I loop-> reach another array, if element not an array, push it to my other array, if yes-> keep looping.
so why is it wrong? because the order might change?

“Loop until there are no more sub-arrays”

“Do nothing with the sub-arrays”

These two together create an infinite loop.

If i push the sub array elements (which aren’t an array)- > to my array, doesn’t it change the original array? and eventually it should stop the looping?

The sub-array may contain sub-arrays

steamrollArray([1, [2], [3, [[4]]]]);

I’ll give u an example this is arr
arr[i] is sub,
i take out arr[i], if it is not an array. which means (arr) changes correct?
if there is another sub arr for arr[i], lets call it arr[i][j].
If i take out an arr[i][j], shouldn’t it mutate arr also? so question is why do I care about the sub arrays if eventually arr should be empty? (if its an array)

I don’t fully understand what your plan is, but you must retain the original order of the elements. You aren’t currently ‘taking out’ anything from arr, so I’m not sure how you are thinking of doing that, but I’m not sure what you are describing will preserve order.