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

Tell us what’s happening:
Hi Guys! I am trying to figure out how to delete the first space or “-”.
I bet it is somehow possible with an if-statement but i really want to do this with a combined function

Your code so far

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

  let arr = title.toLowerCase().split(" ").join("-");

 return arr;
}
// Only change code above this line
console.log(urlSlug(" 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/107.0.0.0 Safari/537.36

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

Link to the challenge:

I just want to confirm this is what you are trying to do? (your description does not match the request from the linked exercise).
The linked exercise says to turn spaces into dashes.

Your sentence says you want to delete the first space or dash??
(if you do mean that, please give an example so it is 100% clear what you want this function to do)

Oh yes. One test starts with a space infront of the first word. Just like " Winter is Coming" ( did a lot of space to see it clearly). But this first space or later the first dash ( because it is converted to a dash ) should not be there.

okay got it.
You don’t want to use the trim function?
https://www.w3schools.com/jsref/jsref_trim_string.asp

1 Like

Till now the courses never told me about this function. Thank you!

I wasn’t sure if you were aware of it or not. (maybe you were avoiding it as part of an exercise for eg.) In general, whenever you need to do something simple, it is good to check online for a function that will do it for you. (rather than re-invent the wheel)

1 Like

Yes that sounds good. The whole time i was like “This is cheating” but now I will do.

But now there is another problem. the test don’t want to pass. I did it with the trim function and mit console.log shows me what the test result should be, but the test is not “checked” or passed. what should I do ?

[spoiler]
// Only change code below this line
function urlSlug(title) {
  let arr = title.trim().toLowerCase().split(" ").join("-"); 
 return arr;
}
// Only change code above this line
console.log(urlSlug(" Winter Is Coming"));

//Output is "winter-is-coming"
[/spoiler]

You could have used .filter()

if you try to log the broken function call you will get this output
winter-is--coming

so your code is replacing the double spaces with 2 dashes but the exercise wants only 1 dash even if the space is doubled

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