Last active
December 17, 2023 10:00
-
-
Save sandrabosk/878f1b7c5c6e46c05245443dae86c3ed to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ************************************************************************************ | |
// 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; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how does this code solve this test? assert.strictEqual(generateHashtag("code" + " ".repeat(140) + "wars"), "#CodeWars")