Back to all solutions

#557 - Reverse Words in a String III

Problem Description

Given a string s, reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.

Solution

/**
 * @param {string} str
 * @return {string}
 */
var reverseWords = function(str) {
  return str.split(/\s/).map(s => s.split('').reverse().join('')).join(' ');
};