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

[Codewars] The Hashtag Generator (5 kyu) / JavaScript

by fluss 2023. 4. 19.

https://www.codewars.com/kata/52449b062fb80683ec000024

 

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:

The marketing team is spending way too much time typing in hashtags.
Let's help them with our own Hashtag Generator!

Here's the deal:

  • It must start with a hashtag (#).
  • All words must have their first letter capitalized.
  • If the final result is longer than 140 chars it must return false.
  • If the input or the result is an empty string it must return false.

 

Examples

" Hello there thanks for trying my Kata"  =>  "#HelloThereThanksForTryingMyKata"
"    Hello     World   "                  =>  "#HelloWorld"
""                                        =>  false
 

코드

function generateHashtag (str) {
  str = str.split(' ').filter(el => el !== "").map(el => el[0].toUpperCase() + el.slice(1)).join("");
  return str.length === 0 || str.length >= 140 ? false : '#' + str;
}

댓글