-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy paththe-hashtag-generator.js
65 lines (51 loc) · 2.08 KB
/
the-hashtag-generator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
function generateHashtag (str) {
if (!str.trim()) return false;
// split word on spaces
const result = '#' + str.split(' ')
.filter(word => word)
.map(word => {
// map over words -> return word uppercased
return word[0].toUpperCase() + word.slice(1);
}).join(''); // join together on empty string
if (result.length >= 140) return false;
return result;
}
function generateHashtag (str) {
if (!str.trim()) return false;
const result = '#' + str.replace(/\s+/g, ' ').split(' ')
.map(word => {
return word[0].toUpperCase() + word.slice(1);
}).join('');
if (result.length >= 140) return false;
return result;
}
function generateHashtag (str) {
if (!str.trim()) return false;
const result = '#' + str.replace(/\s+/g, ' ').split(' ')
.reduce((result, word) => {
return result + word[0].toUpperCase() + word.slice(1);
}, '');
if (result.length >= 140) return false;
return result;
}
function generateHashtag (str) {
if (!str.trim()) return false;
const result = str.trim().split(/\s+/g)
.reduce((result, word) => {
return result + word[0].toUpperCase() + word.slice(1);
}, '');
if (result.length >= 140) return false;
return '#' + result;
}
console.log(generateHashtag(" Hello there thanks for trying my Kata"));
console.log(generateHashtag(" Hello World "));
console.log(generateHashtag(''), false);
console.log(generateHashtag(' '.repeat(200)), false);
console.log(generateHashtag('Do We have A Hashtag'), '#DoWeHaveAHashtag');
console.log(generateHashtag('Codewars'), '#Codewars');
console.log(generateHashtag('Codewars Is Nice'), '#CodewarsIsNice');
console.log(generateHashtag('Codewars is nice'), '#CodewarsIsNice');
console.log(generateHashtag('code' + ' '.repeat(140) + 'wars'));
console.log(generateHashtag('Looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong Cat'), false);
console.log(generateHashtag('a'.repeat(139)), '#A' + 'a'.repeat(138));
console.log(generateHashtag('a'.repeat(140)), false);