What does the filter do in this case?

I found the solution - but by accident.
I dont understand what the .filter() does here.
Can someone explain (in the context of this program).

The case here is that when there is a space before a word, without the filter it doesnt pass, otherwise it does just fine. For the FCC editor to pass the program we need to make sure it doesnt include the space in the front. I thought of filtering it after converting it to a list. Just wrote a filter with a list arg(had no idea what exactly to do then ,it was a loose guess) . For some reason when I pressed “run” , it passed. I have no idea what happened. But I do know what the other parts of the code do.

function urlSlug(title){
let array = title
  .split(" ")
  .filter(list=> list) //what does this do in here???
  .join(" ")

let string = array
  .split(" ")

return string;

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/97.0.4692.71 Safari/537.36 Edg/97.0.1072.55

Challenge: Apply Functional Programming to Convert Strings to URL Slugs

Link to the challenge:

The filter array method allows you to pass every element in the array through a boolean test and only returns those elements where the test returns true. So what is the test in this case? Remember, you don’t necessarily need an operator to perform a boolean test. A simple value can be evaluated as a boolean value. For example, the number 0 evaluated as a boolean is false while any other number is true.

1 Like

ohh, i get it .
so does that mean an empty " " return false?

1 Like

One way you can test whether a value is truthy or falsy (i.e. force it to be evaluated as a boolean) is to prefix it with !!. So you can open up the JS console in your browser’s dev tools and find out for yourself :slight_smile:

1 Like

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