FAQ: Code Challenges: Intermediate JavaScript - reverseArray()

Hi everyone,

I’m looking for some help with this challenge.
I could tackle this out with a simple for loop.
But I was trying a more elegant solution and realized this code doesn’t pass the tests

const reverseArray = array => array.reduceRight((accu, elem) => accu.concat(elem), []) ;

Any idea why ?
Thanks !

Your solution is more advanced than the lesson checker expects. It is looking for an iterative approach using for loop.

Concat will give you a problem when you reverse an array of arrays and try to join them with concat: [[1], [2, 3], [4]]. The test file has a test for this and gives a hint in the error msg. You can replace concat-call with the JS spread-operator to create the new longer array as you are accumulating:

[...accu, elem]

Oh, I did not consider testing with nested arrays.
And did not know about the spread operator, your post is really helpful ! Thank you !

1 Like