Skip to content

Instantly share code, notes, and snippets.

@sandrabosk
Last active December 17, 2023 10:00
Show Gist options
  • Save sandrabosk/878f1b7c5c6e46c05245443dae86c3ed to your computer and use it in GitHub Desktop.
Save sandrabosk/878f1b7c5c6e46c05245443dae86c3ed to your computer and use it in GitHub Desktop.
// ************************************************************************************
// 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(string) {
if (string.trim() === '') return false;
const stringWithCamelCase = string
.split(' ')
.map(word => word.charAt(0).toUpperCase() + word.slice(1))
.join('');
const stringWithHashtag = `#${stringWithCamelCase.trim()}`;
return stringWithHashtag.length > 140 ? false : stringWithHashtag;
}
@timakaa
Copy link

timakaa commented Dec 17, 2023

how does this code solve this test? assert.strictEqual(generateHashtag("code" + " ".repeat(140) + "wars"), "#CodeWars")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment