본문 바로가기
알고리즘/Codewars

[Codewars] Reverse words (7 kyu) / JavaScript

by fluss 2023. 1. 18.

https://www.codewars.com/kata/5259b20d6021e9e14c0010d4

 

Codewars - Achieve mastery through coding practice and developer mentorship

A coding practice website for all programming levels – Join a community of over 3 million developers and improve your coding skills in over 55 programming languages!

www.codewars.com

 

DESCRIPTION:

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"

 

코드

function reverseWords(str) {
  return str.split(" ").map(el =>el.split("").reverse().join("")).join(" ");
}

댓글