Are we not allowed to use the flatMap method on this? All outputs match the expected return statements.
If we are allowed to use Array.isArray, map, slice, etc., then we should be able to use flatMap for this lesson as well?
Your code so far
function steamrollArray(arr) {
// I'm a steamroller, baby
let flattened = arr.flatMap(x => [x]);
console.log(flattened);
return [flattened];
}
steamrollArray([1, [2], [3, [[4]]]]);
Your browser information:
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36
.
Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/intermediate-algorithm-scripting/steamroller
You can use any solution you like as long as it satisfies the tests. The challenge was probably written before the .flatMap
method was added to JS (I wasn’t aware it existed either!)
lasjorg
February 28, 2019, 2:49am
3
It is not returning the correct output, that is why you are not passing.
If you want to use something like this then using flat() is easier. Just use flat()
with a depth of 3 and return its return value.
Thank you! The console output was obfuscating the nested array in the return statements. Running JSON.stringify() on the flatMap output showed the nested structure. Appreciate your help.
kylec
February 28, 2019, 9:13pm
5
Since the flat()
method has already been brought up
FYI: You can use <array>.flat(Infinity)
to completely flatten an array when depth is not known.