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(' ');
Top comments (0)