Apply Functional Programming to Convert Strings to URL Slugs - BUG

Tell us what’s happening:
I was writing this challenge and trying my code, but even though I was certain it was it didn’t pass the first test.

Then I tried googling solutions and none passed the test for some reason.

Apparently, the problem is some bug and just resetting the code fixes the problem.

This link helped me.

Please put some notice until you fix the bug.

Your code so far


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

// Add your code below this line
function urlSlug(title) {
  let slug = title.toLowerCase().split(/\s/).filter(function(i) {
             return i !== "";
             }).join("-");
  console.log(globalTitle);
  return slug;
  
}
// 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/67.0.3396.99 Safari/537.36.

Link to the challenge:

you should use /\s+/ .

this worked for me.try it

this is my solution :

function urlSlug(title) {
return title.toLowerCase().split(" ").filter(function(item){
    return item;
}).join("-");
 
}
1 Like

I did it like this, i could not find a way to remove that first whitespace character in second test " Winter is…" so i used .trim()

Does anybody know how to remove that whitespace before 1st word? thanks :grinning:

solution:

function urlSlug(title) {
  return title.toLowerCase().trim().split(/\s+/g).join("-");
}

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

My working solution:

function urlSlug(title) {

var newTitle = title.split(/\W+/);

var filterTitle = newTitle.filter(function(x){
return /\S/.test(x);

});

return filterTitle.join(“-”).toLowerCase();

};

Hello Everybody!

I don’t know why my solution doesn’t work:

function urlSlug(title) {
return globalTitle.split(" “).map(val => val.toLowerCase()).join(”-");

}

I’ve tested it in the Chrome’s console and it works just fine.

Can anybody explain why it doesn’t work?

Thanks,
Szymon