Functional Programming - Apply Functional Programming to Convert Strings to URL Slugs

Tell us what’s happening:
What’s wrong here?

Your code so far

// Only change code below this line
function urlSlug(title) {
  return title.trim().split(/(\S)\s{1,}(\S)/g).join("-").toLowerCase();
}
// Only change code above this line
(urlSlug("A Mind Needs Books Like A Sword Needs A Whetstone"));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36

Challenge: Functional Programming - Apply Functional Programming to Convert Strings to URL Slugs

Link to the challenge:

split the title into an array of words using the regular expression /\s+/ , which matches one or more consecutive spaces.

Did you try logging out what it is you are matching on?

  console.log(title.trim().match(/(\S)\s{1,}(\S)/g))
  console.log(title.trim().split(/(\S)\s{1,}(\S)/g))

You can also use a site like regex101

Also, trim only removes the whitespace from the beginning and end of the string, not any extra spaces between the words.

sing the reduce() function as mentioned in the problem statement and the trim() function to remove leading and trailing spaces:

function urlSlug(title) {
  return title.trim().split(` `).reduce((string, currentValue) => {
    return string +`-`+currentValue;
  }, );
}

console.log(urlSlug("  A Mind Needs Books Like A Sword Needs A Whetstone "));

toLowerCase() method also has to be used here.
And, also more than 1 space b/w words are causing errors too.

@giochi even though your code has a bug, it is close enough to a solution that I have blurred it.

Instead of posting working solutions, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

I’m sorry, I didn’t mean to spoil it. I wanted to help, but I understand what you’re saying. I will try to follow your advice, and I hope I didn’t ruin their exercise experience. good coding :slight_smile:

Can I know which bug is present, please?

Array produced by the method method:
[ ‘A M’, ‘d N’, ‘s B’, ‘s L’, ‘e A’, ‘d N’, ‘s A’ ]

So, isn’t the array produced by split method should be:
[’ ‘, ‘in’, ‘eed’, ‘ook’, ‘ik’, ’ Swor’, ‘eed’, ’ Whetstone’]

But it isn’t, why?

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.