Apply Functional Programming to Convert Strings to URL Slugs

// Only change code below this line
function urlSlug(title) {

// Use the map method to convert the current string into a lowercased hyphenated version 

// Only change code above this line
urlSlug("A Mind Needs Books Like A Sword Needs A Whetstone");

I am so lost on this one. I’m trying to remain positive when I get stuck on problems but it’s so discouraging when it feels like I can only get through the problems that have examples (aka FCC holding my hand). Then when a problem comes later down the road and they ask to apply what I have learned, I am consistently lost. How can I get through 10 problems then on the 11th one where they take the training wheels off…I am completely lost as if I retained absolutely nothing. Did anyone else feel like this 3 weeks into their journey of becoming a front-end web developer?

Also, can someone help me out with this problem without giving me the answer? I wrote a note of what I think should be done but I don’t know how to go about it

Let’s start out easy and just concentrate on converting to lowercase. Do you know the name of the method you would use to do that?

1 Like

I hope you aren’t too discouraged. Struggling is a normal part of the process, and the fact that you’ve gotten this far into JS after only three weeks is very impressive.

It looks like @bbsmooth beat me to it, so I’ll let the two of you work through this.

Thanks for the reply! I believe it is the toLowerCase( ) method. In order to convert to lowercase would the code look something like this:

title.toLowerCase()

Perfect. And you can string (or chain) functional methods together. Since toLowerCase returns a string, now you can apply a method to that string. So after converting the string to lowercase, what do you want to do with it?

If you need a hint, I would look closely at the two previous challenges to this one.

I’m trying not to be too discouraged. I believe I’m coming up on week 4 soon, I got through the first HTML and CSS section in about 10 days because everything was fairly straightforward, but I have been on the JavaScript Algorithms and Data Structures section for a while now. I’m not in a rush and I don’t have any quit in me, but I will admit it is hard to stay mentally strong when you get through so many problems but when examples are removed from the question you can’t apply the previous concepts as if you aren’t retaining what you’ve learned. Trying not to be too hard on myself tho

1 Like

I know in the previous to lessons (/\d/) and (/\W/) were used. The first one was for digits and the second was for words. So would I use the words one in this way?

return title.split.(/\W/).join("-"); 

It’s awesome that you don’t have any quit in you. I am also glad to see that you are choosing to reach out rather than give up. People study for years to become proficient in this field. It’s not something that happens overnight. If you just keep showing up and engaging with the struggle, I know you will become great at this.

1 Like

That’s pretty close, but I think there can be more than one space between the words. So you need to add something to \W to account for that.

Thank you so much for the kind words! You’re definitely right, can’t expect to be good at something only 3-4 weeks into it. As long as I keep reaching out to this community and avoid looking up answers…I should be fine. It’s going to be a nice feeling when I start to truly understand what I am doing and code with a bit more ease. I am also aware that even professionals struggle in this field, it’s just a part of the territory…can’t get in my feelings about it, just have to keep taking steps forward.

1 Like

I console logged the current code I have and it looks good, for some reason everything isn’t lowercased though

// Only change code below this line
function urlSlug(title) {

title.toLowerCase(); 
return title.split(/\W/).join("-");

}
// Only change code above this line
console.log(urlSlug("A Mind Needs Books Like A Sword Needs A Whetstone"));

UPDATE:

// Only change code below this line
function urlSlug(title) {

title = title.toLowerCase(); 
return title.split(/\W/).join("-");

}
// Only change code above this line
console.log(urlSlug("A Mind Needs Books Like A Sword Needs A Whetstone"));

This is very close. Only one doesn’t pass because of the spacing. I completely blanking on how to fix that issue

You should combine those two lines into one so that you just have one line that is a return statement. That’s one of the advantages of functional programming, it makes things more concise.

title.toLowerCase() returns a new string that is all lower case. You can then immediately chain the split and join methods after it. You don’t need two separate lines to do that.

You current issue is that the tests will give you a string that has more than one space between two words. So you need to take that into account in your split pattern. Do you remember how to do that?

Match Characters that Occur One or More Times

Fixed it to make everything in one line.

return title.toLowerCase().split(/\W/).join("-");

I remember the lesson you sent a link to and how /s+/g would account for all the letter s’s in the string. Could I use a similar method to account for all the spaces in a string?

What does the + mean when you use it in a pattern?

The + makes sure it goes through and checks everything in the string. Similar to how i++ in a for loop means it’s going up by one. Probably a bad explanation but was that close?

The first line in the lesson I linked to above:

“Sometimes, you need to match a character that appears one or more times in a row.”

That’s what the + does in a pattern. It allows you to match one or more of the same character in a row.

First, I should have mentioned this earlier, you should use something other than \W in the split pattern. It works for this this challenge because the test strings do not use any non-word characters. But really it is not the most appropriate character class to use here. Words are separated by spaces, so your pattern should be looking for just spaces.

Match Whitespace

So if you want to match one or more whitespace characters in a row then what would you add to do that?

return title.toLowerCase().split(/\s+/).join("-");

This almost fixed everything but for some reason it only fixed extra spaces in the middle of the string and not the beginning. " Winter Is Coming" comes out like
-winter-is-coming

Yep, that’s your last issue. You’ll need to trim those spaces from the beginning and the end of the string.

That’s the hint.

1 Like

Ahhhhhhhh thank youuu!! Forgot all about the trim( ) method. Appreciate the help, no way I could get through this without this forum

eturn title.toLowerCase().trim().split(/\s+/).join("-");

passed