見習村05 - The Hashtag Generator

05 - The Hashtag Generator

Don’t say so much, just coding…

Instruction

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.

Example

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

Ruby

Init

1
2
3
def generateHashtag(str)
# ...
end

Sample Testing

1
2
3
4
5
6
7
8
9
10
Test.assert_equals(generateHashtag(""), false, "Expected an empty string to return false")
Test.assert_equals(generateHashtag(" " * 200), false, "Still an empty string")
Test.assert_equals(generateHashtag("Do We have A Hashtag"), "#DoWeHaveAHashtag", "Expected a Hashtag (#) at the beginning.")
Test.assert_equals(generateHashtag("Codewars"), "#Codewars", "Should handle a single word.")
Test.assert_equals(generateHashtag("Codewars Is Nice"), "#CodewarsIsNice", "Should remove spaces.")
Test.assert_equals(generateHashtag("Codewars is nice"), "#CodewarsIsNice", "Should capitalize first letters of words.")
Test.assert_equals(generateHashtag("code" + " " * 140 + "wars"), "#CodeWars")
Test.assert_equals(generateHashtag("Looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong Cat"), false, "Should return false if the final word is longer than 140 chars.")
Test.assert_equals(generateHashtag("a" * 139), "#A" + "a" * 138, "Should work")
Test.assert_equals(generateHashtag("a" * 140), false, "Too long")

Javascript

Init

1
2
3
function generateHashtag (str) {
// Code here
};

Sample Testing

1
2
3
4
5
6
7
8
9
10
Test.assertEquals(generateHashtag(""), false, "Expected an empty string to return false")
Test.assertEquals(generateHashtag(" ".repeat(200)), false, "Still an empty string")
Test.assertEquals(generateHashtag("Do We have A Hashtag"), "#DoWeHaveAHashtag", "Expected a Hashtag (#) at the beginning.")
Test.assertEquals(generateHashtag("Codewars"), "#Codewars", "Should handle a single word.")
Test.assertEquals(generateHashtag("Codewars Is Nice"), "#CodewarsIsNice", "Should remove spaces.")
Test.assertEquals(generateHashtag("Codewars is nice"), "#CodewarsIsNice", "Should capitalize first letters of words.")
Test.assertEquals(generateHashtag("code" + " ".repeat(140) + "wars"), "#CodeWars")
Test.assertEquals(generateHashtag("Looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong Cat"), false, "Should return false if the final word is longer than 140 chars.")
Test.assertEquals(generateHashtag("a".repeat(139)), "#A" + "a".repeat(138), "Should work")
Test.assertEquals(generateHashtag("a".repeat(140)), false, "Too long")

Thinking

想法(1): 須先思考如何處理傳進來的值為空白的狀況,會影響應該判斷長度為 140 || 139
想法(2): 將值切開分群然後轉為大寫再組回來,並且再加上 #hashtag

https://ithelp.ithome.com.tw/upload/images/20200920/20120826uApzNhLLnM.jpg
圖片來源:Unsplash NordWood Themes

Hint & Reference

Solution

Ruby

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# Solution 1
def generateHashtag(str)
string = str.split.map(&:capitalize).join
if (string.empty? || string.length > 139)
false
else
"##{string}"
end
end

# Solution 2
def generateHashtag(str)
return false if str.strip.empty?
str = str.split(" ").map(&:capitalize).join.prepend("#")
str.length > 140 ? false : str
end

Javascript

1
2
3
4
5
6
7
8
9
10
11
12
// Solution 1
function generateHashtag (str) {
let wordArray = str.split(' ').filter(char => char !== "");
let result = "#";

result = result + wordArray.map(word => {
let capitalizedWord = word.charAt(0).toUpperCase() + word.slice(1);
return capitalizedWord;
}).join('');

return wordArray.length === 0 || result.length > 140 ? false : result
};