Skip to content

Instantly share code, notes, and snippets.

@dimasandhk
Created June 15, 2021 11:01
Show Gist options
  • Save dimasandhk/b435398cf39eddf3af91efc3c578edc7 to your computer and use it in GitHub Desktop.
Save dimasandhk/b435398cf39eddf3af91efc3c578edc7 to your computer and use it in GitHub Desktop.
Kata Solution (Codewars) 5 KYU The Hashtag Generator with Javascript
// ************************************************************************************
// https://www.codewars.com/kata/the-hashtag-generator
// 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.
// Examples:
// " Hello there thanks for trying my Kata" => "#HelloThereThanksForTryingMyKata"
// " Hello World " => "#HelloWorld"
// "" => false
// ************************************************************************************
function generateHashtag(str) {
if (!str.trim()) return false;
const capitalize = str.replace(/\s+/g, " ").trim().split(" ")
.map((word) => `${word[0].toUpperCase()}${word.slice(1)}`);
const result = ["#", ...capitalize].join("");
return result.length > 140 ? false : result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment