Apply-functional-programming-to-convert-strings-to-url-slugs

Apply-functional-programming-to-convert-strings-to-url-slugs
0

I had no idea that the .trim was a method to use. that helped me out a ton thanks.
to make the code shorter you could put it all on one, line… It doesn’t really make it shorter but in my opinion it makes you look like a ninja. this is how I solved it


> function urlSlug(title) {
>   return title.toLowerCase().trim().split(" ").join("-");  
> }

With .filter((item)=>item!='') are you filtering out anything character that isn’t equal to ‘’?

I’m confused what that part of the code is doing. My (non-working) solution is:

var globalTitle = "Winter Is Coming";

// Add your code below this line
function urlSlug(title) {
  var lower = title.toLowerCase();
  var arr1 = lower.split(" ");
  var arr2 = arr1.join("-");
    
  return arr2;
}
// Add your code above this line


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

and it seems like the only difference (besides your code being more condensed) is the use of filter. Can you please explain what filter is doing here? Thanks!

Take a look at the filter method documentation first and then create a new topic on what questions or clarifications you still need about it.

your code fails if between 2 words is 2 or more spaces.
rec: use split(/\s+/) instead of split(" ")

that’s good to know, thanks.

that’s good to know, thanks.

NP.

function urlSlug(title) {
var first = title.trim().split(/\s+/);
var lowercase = [];
for (var i=0;i<first.length;i++){
lowercase.push(first[i].toLowerCase());
}
return lowercase.join("-");
}

Or even easier

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

// the global variable
var globalTitle = “Winter Is Coming”;

// Add your code below this line
function urlSlug(title) {

var splited = title.trim().toLowerCase().split(/\W+/).join("-");

return splited;
}
// Add your code above this line

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

alert(urlSlug(" Winter Is Coming")); // should return

So I passed the test, but just got lucky with filtering out the white space in the beginning of the sentence and the double space between “is” and “coming”. Below is my solution. Can someone please break down what is going on within my filter(). Thank you.

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

// Add your code below this line
function urlSlug(title) {
  
  return title.toLowerCase().split(/\s/).filter(i => i!=="").join("-");
}
// Add your code above this line

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

Just check this,falva26.
console is very useful.

1 Like

Why does it say “The globalTitle variable should not change.” there, although I did not change this variable?

My code:

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

// Add your code below this line
function urlSlug(title) {

return title.toLowerCase().split(/\W/).filter((item) => item != ‘’).join("-");

}
// Add your code above this line

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

Can you help me?

function urlSlug(title) {
  var copytitle = title.split(/\W/).map(x => x.toLowerCase()).join('-');
  return copytitle;
  
}
Why it  is failing case "urlSlug(" Winter Is  Coming") should return "winter-is-coming"

It is failing the case urlSlug(" Winter Is Coming") should return “winter-is-coming”

Hello, I did it by using the following functions:
-try and lowercase everything;
-split by any NOT letter character;
-filter to find out only what is different by ’ ’ (black space);
-finally, join everything with a -.

Hope this helps!

I initially used a filter to solve this, then realised if I could just split it better, like you did, then that could remove the need for filter.

Your regex works well for the current tests, but your regex will still allow underscores to slip through. I ended up with the following code, think it should have everything covered.

return title.trim().toLowerCase().split(/[^a-z]+/).join("-");
Summary

I am probably late but this is how I solved this problem.
var globalTitle = “Winter Is Coming”;

// Add your code below this line
function urlSlug(title) {

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

}
// Add your code above this line

var winterComing = urlSlug(globalTitle);

My example is like this but didnt work and still wondering why??

  return title.split(/\W/).trim().join("-").toLowerCase()

title.split(/\W/) is an array, you need to use trim() on a string not an array. JavaScript String trim() Method

Try using trim() directly on the string, like title.trim()

then you are still not considering " Winter Is Coming", that have two spaces between words, and it will result in winter-is--coming

try solving this on your own, if you can’t ask again :slight_smile:

1 Like

I found the mistake, I should have added + on W so it is W+ which means find one or more spaces, and changed trim to be the first method

I used match for any non white space repeating characters.
However, I am curious which is more efficient between using Filter, Map, Trim, or etc.

return title.toLowerCase().match(/\S+/g).join('-');