Don't say so much, just coding...
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.
" Hello there thanks for trying my Kata" => "#HelloThereThanksForTryingMyKata"
" Hello World " => "#HelloWorld"
"" => false
def generateHashtag(str)
# ...
end
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")
function generateHashtag (str) {
// Code here
};
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")
想法(1): 須先思考如何處理傳進來的值為空白的狀況,會影響應該判斷長度為 140 || 139
想法(2): 將值切開分群然後轉為大寫再組回來,並且再加上 #hashtag
Ruby
JavaScript
# 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
// 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
};