DEV Community

Justin
Justin

Posted on

JS Challenge: Reverse the characters of each word in a string

The solution is to create an array of the characters in the string by using the split() function, reverse the array and then join the characters back into a string using join() function.

str = 'Once upon a time there was clean fresh air';
str.split(' ').map(word => 
  word.split('').reverse().join('')
).join(' ');

//To reverse the order of words in the string too

str.split(' ').map(word => 
  word.split('').reverse().join('')
).reverse().join(' ');

Enter fullscreen mode Exit fullscreen mode

Top comments (0)