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

[Codewars] Reversing Words in a String (8 kyu) / JavaScript

by fluss 2023. 4. 29.

https://www.codewars.com/kata/57a55c8b72292d057b000594

 

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:

You need to write a function that reverses the words in a given string. A word can also fit an empty string. If this is not clear enough, here are some examples:

As the input may have trailing spaces, you will also need to ignore unneccesary whitespace.

Example (Input --> Output)

"Hello World" --> "World Hello"
"Hi There." --> "There. Hi"

Happy coding!

 

코드

function reverse(string){
  return string.split(' ').reverse().join(' ');
}

댓글