Question on using regex for map()

I think my idea would work but I suspect some problems in properly implementing it:

I want to map() a new array in lowercase() with hyphens between words. I want map() to create a new array based on that regex.
I think my regex does that (right?). I’m a bit confused about how to implement that using map. test() is a boolean and I’m not trying to match anything. What’s below is a shot in the dark but I am very open to hearing where my assumptions fall flat and are in need of revision.

Thank you
Nick
Tell us what’s happening:
Describe your issue in detail here.

Your code so far


// Only change code below this line
function urlSlug(title) {
let change = /\s{-}/gi
title.map((arr)=>{
return change;
});


}
// Only change code above this line

Your browser information:

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

Challenge: Apply Functional Programming to Convert Strings to URL Slugs

Link to the challenge:

you cant map title because its not an array, its a string.

So I would use .split() first?

What are you trying to do? What do you want the array to contain after you’re done?

I want the array to contain the string but lowercased and the empty spaces replaced with “-”

You don’t need a regular expression to replace a single character with another character.

A requirement of the challenge is that you can’t use replace(). They suggest one of the higher order methods (map, reduce). I was trying map() because it returns a new array so I thought that was the most logical given the type of problem presented.

If you can’t use replace(), then I’m not sure that a regular expression is going to be much help.

I would skip regex all together. This is the functional programming section. I would just use functional techniques. Think big picture here. Can you describe the steps you would use by hand?

I’m posting my code below. I have my plan sketched out, but I’m hitting a roadblock with filter. It’s not removing the spaces as I think it should. I making an incorrect assumption with filter() and/or typing error. I’m not sure which, but if y’all can point something out I’d appreciate it.

// Only change code below this line

function urlSlug(title) {

  //copy array, change case and change into array

let newTitle = title.slice().toLowerCase().split(' ');

console.log(newTitle);

//filter out empty spaces

let filtered = newTitle.filter(word=> word != ' ');

console.log(filtered);

//connect different elements with join('-')

}

// Only change code above this line

If you split on spaces, then there are no longer any spaces. You might be seeing some empty strings in your output though. Those are slightly different than spaces!

Side Note: You should use !== over != in general

Other Side Note: Generally you probably want to used const over let for arrays

Other Other Side Note: Since toLowerCase() returns a new string rather than mutating the original string, you don’t need to slice() a copy of the string.

1 Like