Given a string, reverse each word in the sentence

Technology CommunityCategory: JavaScriptGiven a string, reverse each word in the sentence
VietMX Staff asked 3 years ago
Problem

For example Welcome to this Javascript Guide! should be become emocleW ot siht tpircsavaJ !ediuG

var string = "Welcome to this Javascript Guide!";

// Output becomes !ediuG tpircsavaJ siht ot emocleW
var reverseEntireSentence = reverseBySeparator(string, "");

// Output becomes emocleW ot siht tpircsavaJ !ediuG
var reverseEachWord = reverseBySeparator(reverseEntireSentence, " ");

function reverseBySeparator(string, separator) {
  return string.split(separator).reverse().join(separator);
}