Apply Functional Programming to Convert Strings to URL Slugs

Apply Functional Programming to Convert Strings to URL Slugs
0.0 0

#1

Tell us what’s happening:
This passes all of the tests except: urlSlug(" Winter Is Coming") should return “winter-is-coming”.
Note that there is two spaces between Is and Coming in the input string above.
I added the + sign to my regular expression in the split method to account for this additional space, and when I enter the code as it is in repl.it > javascript it returns “winter-is-coming”. Thus, it seems to me that the code should pass all the tests on freeCodeCamp, but it doesn’t pass the one I’ve noted.
Please, help. Thanks!

Edit: Of course, the function can be reduced to the single line return title.toLowerCase().split(/\W+/).join('-');. I have it separated for bug control and clarity as I’m learning.

Your code so far


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

// Add your code below this line
function urlSlug(title) {
  var little = title.toLowerCase();
  var arr = little.split(/\W+/);
  var strAct = arr.join('-');
  return strAct;
}
// Add your code above this line

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

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 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


#3

…split(/\s+/).filter(x =>{return x!==""}).join(’-’)
that’s how i solved it…


#4

Yea that does solve it. Thanks! Still curious about why mine doesn’t solve if you have any ideas?


#5

/\W+/ “#Winter#Is##Coming” selects all #. I tried /\b\W+/, it selects
“_Winter#Is##Coming”, you’ll get " winter-is-coming" instead of
“-winter-is-coming”. But " winter’ still has one space. I don’t know how to use Regular Expression to not select that remaining space. Easier to use filter instead…maybe it can’t be solved using Regular Expression alone? …split(’ ‘).filter(x =>{return x!==’’}).join(’-’) solves it too…