1

I'm trying to solve this coding problem please don't solve it for me, but for some reason I'm not able to split, map, join. The join portion seems to be failing from my tests. Could someone give me a hit and point were I'm messing things up?

function generateHashtag(str) {
  if (str === "" || str.length > 140) return false //validation
  let words = str
    .split(" ") //split into words
    .map((word) => {
      word
        .split("") //split into letters
        .map((character, i) => {
          //capitalize only the first letter of the word
          character.toLowerCase()
          if (i === 0) character.toUpperCase()
        })
        .join("") //rejoin letters
      // console.log(word)
    })
  .join("")
  // console.log(words)
  return "#" + words
}
BTW, this is the task:

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

Thanks in advance!!

santsleo
  • 131
  • 1
  • 5
  • This question is probably off-topic. By the way, you forgot to return from the `.map` callback... – D. Pardal Jul 11 '20 at 16:54
  • 2
    You have to *return* values from the `.map()` callback function. The `.toLowerCase()` and `.toUpperCase()` methods do not change the original strings; they return a new string. – Pointy Jul 11 '20 at 16:55
  • 1
    This is what's wrong with your code: [Why doesn't my arrow function return a value?](https://stackoverflow.com/q/45754957/9867451) – ibrahim mahrir Jul 11 '20 at 16:55

3 Answers3

2

First we need to know how this methods works.

In your code are using .toLowerCase and .toUpperCase these two methods do not modify the original string, returns a new modified string.

In your .map you have to return values to callback function, if you want to modified the current array just use .forEach

Diego Vieira
  • 266
  • 3
  • 12
1

you forgot to add return in your .map()

    return word
        .split("") //split into letters
        .map((character, i) => {
          //capitalize only the first letter of the word
         if (i === 0) return character.toUpperCase()
         return character.toLowerCase()
        })
        .join("") //rejoin letters
Michel Vorwieger
  • 692
  • 5
  • 14
0

Ok thanks for everyone that helped, here's how I ended up solving it (I was over engineering it before):

function generateHashtag(str) {
  if (str === "" || str.length > 140) return false //validation
  let words = str
    .toLowerCase()
    .split(" ") //split into words
    .map((word) => word.charAt(0).toUpperCase() + word.slice(1))
    .join("")
  return `#${words}`
}
santsleo
  • 131
  • 1
  • 5