The Hashtag Generator

samuel Jo·2022년 12월 5일
0

codewars

목록 보기
12/46

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

일단 이문제를 풀기 이전에 String.trim() 과 String.slice() .split() join()을 알아야 할 필요가 있다.

function generateHashtag(str) {
    const output = str.trim().length > 0 &&
        '#' + str.split(' ').map(
            // &&연산자를 안넣으면 undfined부터도는 맵함수의 특성상 &&연산자를 넣어준다.
            word => word && (word[0].toUpperCase() + word.slice(1))).join('');

    return output.length < 141 && output;
}


이런식으로 짜봤다. 처음에 짠건

이거였으나 뭔가 짜고도 내가 보기 힘들어보였다. return부분이 헷갈린다랄까..

profile
step by step

0개의 댓글