Problem
- The marketing team is spending way too much time typing in hashtag.
- 마케팅 팀이 해시태그를 입력하는 데 너무 많은 시간을 소비하고 있다.
- Let’s help them with our own Hashtag Generator.
- Hashtag Generator로 마케팅 팀을 도와보자.
- 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’.
- 최종 결과가 140자를 초과하면 ‘false’를 반환한다.
- If the input or the result is an empty string it must return ‘false’.
- 입력 또는 결과가 빈 문자열이면 ‘false’를 반환한다.
Solution 01
function generateHashtag(str) {
str = str.split(' ').reduce((total, i) => {
return total + (i ? i[0].toUpperCase() + i.slice(1) : '');
}, '#');
return str === '#' || str.length > 140 ? false : str;
}
generateHashtag(''); // false
generateHashtag(' '); // false
generateHashtag('codewars'); // #Codewars
generateHashtag('code' + ' '.repeat(140) + 'wars'); // #CodeWars
generateHashtag('Codewars'); // #Codewars
generateHashtag('Hello World'); // #HelloWorld
generateHashtag('a'.repeat(139)); // #A + 'a'.repeat(138)
generateHashtag('a'.repeat(140)); // false
Solution 02
function generateHashtag(str) {
let hash = '#';
str.split(' ').map(i => {
hash += i.charAt(0).toUpperCase() + i.slice(1);
});
return hash === '#' || hash.length > 140 ? false : hash;
}
generateHashtag(''); // false
generateHashtag(' '); // false
generateHashtag('codewars'); // #Codewars
generateHashtag('code' + ' '.repeat(140) + 'wars'); // #CodeWars
generateHashtag('Codewars'); // #Codewars
generateHashtag('Hello World'); // #HelloWorld
generateHashtag('a'.repeat(139)); // #A + 'a'.repeat(138)
generateHashtag('a'.repeat(140)); // false
Solution 03
function generateHashtag(str) {
str = str.split(' ').map(i => i.slice(0, 1).toUpperCase() + i.slice(1)).join('');
return str.length < 1 || str.length >= 140 ? false : '#' + str;
}
generateHashtag(''); // false
generateHashtag(' '); // false
generateHashtag('codewars'); // #Codewars
generateHashtag('code' + ' '.repeat(140) + 'wars'); // #CodeWars
generateHashtag('Codewars'); // #Codewars
generateHashtag('Hello World'); // #HelloWorld
generateHashtag('a'.repeat(139)); // #A + 'a'.repeat(138)
generateHashtag('a'.repeat(140)); // false