loading...

re: Daily Challenge #15 - Stop gninnipS My sdroW! VIEW POST

FULL DISCUSSION
 

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.

 

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].

code of conduct - report abuse