Skip to content

Latest commit

 

History

History
38 lines (28 loc) · 961 Bytes

the-hashtag-generator.md

File metadata and controls

38 lines (28 loc) · 961 Bytes

The Hashtag Generator 5 Kyu

LINK TO THE KATA - STRINGS ALGORITHMS

Description

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.

Solution

const capitalize = str => {
  return `${str.charAt(0).toUpperCase()}${str.slice(1)}`
}

const generateHashtag = str => {
  if (str.trim() === '') return false

  const hashtag = `#${str
    .split(/\s+/)
    .map(str => capitalize(str))
    .join('')}`

  return hashtag.length > 140 ? false : hashtag
}