DEV Community

Discussion on: Daily Challenge #15 - Stop gninnipS My sdroW!

Collapse
 
alvaromontoro profile image
Alvaro Montoro

JavaScript

const spinner = sentence => sentence.split(' ')
                                    .reduce((acc, val) => `${acc} ${val.length > 4 ? val.split('').reverse().join('') : val}`, '')
                                    .trim();

Using reduce to generate a string from the array, but then I have to use trim as I end up with a space at either end :-/

Live demo on CodePen.

Collapse
 
willsmart profile image
willsmart

Nice one!
If you replace the trim() with a substring(1) then it'll work with strings that have spaces in front and behind since you're reliably adding a single space to the front, none to the back.

See the warning here re thesplit('') call. Seems the recommended way now is [...string].