The recursion solution i came up with for the Steamroller problem is not being accepted

Tell us what’s happening:
i am trying to solve this challenge, i approached this problem with a recursion and when i console log the result seems fine but freecodecamp is not accepting it.

Your code so far


function steamrollArray(arr) {
let result = []
let index = [];
let obj = []
if(!Array.isArray(arr)){

  result.push(arr)
}
else {

  arr.map(item=>{
    
    if(item == '[object Object]'){
      index.push(JSON.stringify(item))
    }
    else{
      
    index.push(steamrollArray(item))
    }
    
    
  })
}


result+=index
obj = result
console.log(obj)
return obj;
}

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

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36.

Challenge: Steamroller

Link to the challenge:

const result = steamrollArray([[["a"]], [["b"]]]);
console.log(result) // a, b
console.log(typeof result); // string

Because you’re returning a string, not an array.

The test says:

steamrollArray([[["a"]], [["b"]]]) should return ["a", "b"].

The result must be an array. Flattening an array means you end up with an array.

1 Like

oh wow , so all i need to do is change it to array? thanks for the reply

Not really. Your solution needs a bit of work.
Start with an empty array.
Filter the given array and push the values you want to keep into the newly created one.
Try that if you like.

1 Like

I think most solutions never create it as a string in the first place. For most solutions, they just have an array and push on the elements as they’re found.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.