Steamroller - Recursive method - Please help check my code

Steamroller - Recursive method - Please help check my code
0

#1

Tell us what’s happening:
Hello,

My code does not work. the browser is telling me that arr[i] does not exist.
Am I missing something here? It’s been a long day and I’ve stared at this problem for way too long… I think my brain needs a reboot…

Please help!

Thanks in advance.

Best,
Julie

Your code so far

function steamrollArray(arr) {
var explored = [];



for(i=0; i<arr.length;i++){
var counter = 0;
while(arr[i].length>counter){
  if (Array.isArray(arr[i])){
steamrollArray(arr[i]);
  
      
  }// if

else{
  explored.push(arr[i]);
}  
  
  counter++;
  
}// while
    
  }// for i

 return explored;
}// function

steamrollArray([1, [2], [3, [[4]]]]);```
**Your browser information:**

Your Browser User Agent is: ```Mozilla/5.0 (Windows NT 10.0; WOW64; rv:56.0) Gecko/20100101 Firefox/56.0```.

**Link to the challenge:**
https://www.freecodecamp.org/challenges/steamroller

#2

I’ve modified my code to below:

function steamrollArray(arr) {
var explored = [];

for(i=0; i<arr.length;i++){
  if (Array.isArray(arr[i])){
steamrollArray(arr[i]);
 }

else{
  explored = explored.concat(arr[i]);
} }

 return explored;
}
steamrollArray([1, [2], [3, [[4]]]]);

#3

I’ve edited your post for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


#4

Your function returns a value. When you are calling it recursively you are not using that returned value, so your recursive call isn’t affecting you end result.

spoiler

explored = explored.concat(steamrollArray(arr[i]));


#5

Ah i see! thanks so much for your help! that cleared it up for me!