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

Tell us what’s happening:

HI,

As often the code works on my text editor and does not work on fcc.

This happens to you too?

Your code so far


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

// Add your code below this line
function urlSlug(title) {
let tab = [...globalTitle]
   
  tab = tab.join("").trim();
  
  tab = tab.split(/\s/);
  
  return tab.join("-").toLowerCase();
  
}
// Add your code above this line

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

Your browser information:

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 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

Your function has title as parameter but you don’t use it. Calling again the function with an other value passed in will make the function not give desired result. Use the parameter of the function instead of the global variable, it is the only way to have a reusable function.

I don’t get why you are passing the string inside an array and then changing the array to a string.

Anyway your code is not able to handle the case with more consecutive spaces, for example

urlSlug("Winter    is coming"); // this results in "winter---is-coming"
2 Likes

yes after reflection I thought about

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