I think this should work but it is not

Tell us what’s happening:
Describe your issue in detail here.

  **Your code so far**

// Only change code below this line
function urlSlug(title) {
let arr=title.slice(" ");

let arr2=[]
for(let i=0;i<arr.length;i++){

if(arr[i]===' '){
    if(arr[0]==' '){
let str2=arr2.join("");
str2=str2.split(" ");
return str2.join("");
// Only change code above this line
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/96.0.4664.110 Safari/537.36

Challenge: Apply Functional Programming to Convert Strings to URL Slugs

Link to the challenge:

The test cases are given on the left (Desktop) - this code is failing the " winter is coming" test.
If I use that string, indeed I get an error. So your code is unable to deal with leading spaces.

1 Like

true but it should remove leading spaces since i am using split 2 times? what could be the fix?

Best bit of advice I can give is to add the following as the first line in you for loop:


It will make it really easy to spot the whitespace problem you haven’t noticed yet!


Was that intentional to use slice method in let arr=title.slice(" "); ?
I believe you should’ve used split instead.

Your code is really confusing.

Rather I would suggest doing it in the following way.

  1. Split the title into words.
  2. Filter all the white spaces.
  3. Convert the array back to a string separated by -.
  4. Finally convert the string into lower case.

I did add this but there is no white space on 0 index the output shows --|W|-- as first element.

I get this in my console when I log " Winter is coming"

--| |--
--| |--
--| |--

look at what is between the last two words!

1 Like

First, .split() doesn’t remove anything. Second you use .split(" ") on str2, which you construct to not contain any spaces, so it’s not doing anything.
There is a general issue with methods you don’t seem to understand. Your slice function is literally doing nothing. And the if-case with arr[0].shift() doesn’t do what you want it to. On top of being logically wrong, the method itself doesn’t exist in this context and would throw an error, IF the title starts with a space.

Just test your code with " Winter is coming " and it will fail.
Then test "Winter is coming " and see how it’s also looking wrong.

1 Like

Also arr[0].shift(); will never work unless arr[0] is an array(i.e. multi dimentional array). In this case, it is not.
shift method works on the array.

1 Like

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