Reverse Words

Lee·2022년 6월 18일

Algorithm

목록 보기
19/92
post-thumbnail

❓ Complementary DNA

Q. Complete the function that accepts a string parameter, and reverses each word in the string. All spaces in the string should be retained.

Examples
"This is an example!" ==> "sihT si na !elpmaxe"
"double spaces" ==> "elbuod secaps"

✔ Solution

function reverseWords(str) {
  // Go for it
  revStr = str.split("").reverse().join("").split(" ").reverse().join(" ");

  return revStr;
}
profile
Lee

0개의 댓글