DEV Community

loading...

Discussion on: Interview Qs Decoded - # 2

Collapse
mikeyglitz profile image
mikeyGlitz

For those who favor a more functional approach:

const str = 'Please reverse me';
const reverse = str.split('').reduce((acc, curr) => [curr, ...acc], []).join();
console.info(reverse);

Output:

em esreveR esaelP
Collapse
cat profile image
Cat Author

Thanks for sharing, but one of the stipulations was that you can’t use reduce! 😅

Collapse
mikeyglitz profile image
mikeyGlitz

My bad -- missed that part.

Thread Thread
cat profile image
Cat Author

That’s okay! Your solution is pretty slick. I was thinking of using reduce until he said not to haha.