Algorithm 1 - [JS] The Hashtag Generator

luneah·2021년 12월 4일
0

Algorithm CodeKata

목록 보기
1/13
post-thumbnail

The Hashtag Generator

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

📌 Needs ) 해시태그(#)로 시작해야 함. 모든 단어는 첫 글자가 대문자여야 함.
최종 결과가 140자보다 길면 false 반환, 입력 또는 결과가 빈 문자열이면 false 반환.

📁 Sol )

function generateHashtag (str) {

  var hashtag = str.split(' ').reduce(function(tag, word) {
    return tag + word.charAt(0).toUpperCase() + word.substring(1);
  }, '#');
  
  return hashtag.length == 1 || hashtag.length > 140 ? false : hashtag;
}
profile
하늘이의 개발 일기

0개의 댓글