Struggling with the CodeWars challenge The Hashtag Generator :
The marketing team needs help creating hashtags fast.
Let's simplify things with our own Hashtag Generator!Here's what we need:
- It should begin with a hashtag (
#
).- All words should be capitalized.
- If the final result exceeds 140 characters, return
false
.- If either the input or output is empty, return
false
.Examples
" Hello there thanks for trying my Kata" => "#HelloThereThanksForTryingMyKata" " Hello World " => "#HelloWorld" "" => false
This is my solution:
function generateHashtag (str) {
if (str == "") {return false;}
else
{
let text = str.trim();
const myArray = text.split(" ");
let word ="";
let finalResult = ""
for(let i=0; i< myArray.length; i++)
{
word = myArray[i];
word = word.charAt(0).toUpperCase() + word.slice(1);
finalResult =finalResult + word;
}
if(finalResult.length >140){return false;}
else {return "#"+finalResult;}
}
}
This is the error message I encountered:
Expected an empty string to return false: expected '#' to equal false
I'm puzzled by this error, as I have covered the check for an empty string in my code.