REGEX/.concat() query regarding Apply Functional Programming to Convert Strings to URL Slugs

Tell us what’s happening:

I’m a bit stuck on a final test for this one.
I have a few things I’m considering, but I don’t know if they’re the correct path to follow and I’m not sure how to implement them without messing up what I think is a pretty satisfactory chain of methods.

I have an idea that regex might work and I’ve thought about /^\s/, but I’ve not found any luck with it so far. I’ve used it within the chain of methods, but that messes up what I’ve got going with join().

I also have thought about .concat() but I don’t really know how I would use that but it seems better than filter() and map() which seem unnecessary considering what I’ve already been able to accomplish.

What am I missing?

Your code so far


// the global variable
var globalTitle = "Winter Is Coming";

// Add your code below this line
function urlSlug(title) {
  var strTitle = title.split(" ").join("-").toLowerCase();
 console.log(strTitle);
return strTitle;

}
// Add your code above this line

var winterComing = urlSlug(globalTitle); // Should be "winter-is-coming"

Your browser information:

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

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/functional-programming/apply-functional-programming-to-convert-strings-to-url-slugs/

try to see the output of console.log(" Winter Is Coming".split(" ")); and (" Winter Is Coming".split(" ").join("-"); (this is the string you are not passing) and consider what you may need to change to solve this too. What’s in excess here? what would you need to remove? And then, how? (if you tell the what, but don’t know the how, you are welcome to ask suggestions on the methods you could use)

EDIT: sorry, missed a piece. Why do you think filter() wouldn’t work?

  • concat join together two different arrays (how would you be able to avoid the empty ones?)
  • map change each element in the array (to what would you change them?)
  • filter returns an array of only the items that respect a certain parameter

split takes a regex-arg. You could split by /\s*/ where \s looks for any whitespace character and * will check for zero or more times.

1 Like

I’m trying out filter() since I’d like to get more practice in on that. I’m really thinking regex is the way to go and I think I have the basic format correct but there are some details I’d like some help with.

  1. Do I need enclose the regex within a variable? Would this allow it to be processed as opposed to leaving it out there raw?
    2, Am I doing too much work here? I feel as if I can solve this in a much simpler manner in terms of regex.

Thank you. My code is below.
function urlSlug(title) {
var strTitle = title.split(" “).join(”-").toLowerCase();
var strStr = strTitle.filter((myStr)=>{
return /^\s*/;
});
return strTitle;

You need to use filter() on an array, not a string. The correct sequence would be to use filter() before you use join(). Also, filter needs a callback that returns true or false. If it returns a string or a number it will count as truthy and nothing will be filtered

I’m trying out filter() since I’d like to get more practice in on that. I’m really thinking regex is the way to go and I think I have the basic format correct but there are some details I’d like some help with.

  1. Do I need enclose the regex within a variable? Would this allow it to be processed as opposed to leaving it out there raw?
    2, Am I doing too much work here? I feel as if I can solve this in a much simpler manner in terms of regex.

You don’t need to use filter here. Also remove the ^ from your Regex, ^ will look at the beginning of the string only. Where as you would want to check the whole string. Better to use * to look for zero or more times or + to look for one or more times.

title.toLowerCase().trim().split(/\s+/).join('-');
1 Like

Almost exactly what I did, except I did the lowercase at the end, but tomato tomahto.