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

Tell us what’s happening:
Hi everyone,

It seems like my solution is working fine on console. But I am not passing one of the test cases. This could be a bug, but I think it could also be something I messed up. Can someone help please? Thanks.

Your code so far



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

// Add your code below this line
function urlSlug(title) {
  return title.trim().toLowerCase().split(" ").join("-");
  
}
// 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/65.0.3325.181 Safari/537.36.

1 Like

This one below is the test case I am failing.

urlSlug(" Winter Is  Coming") should return "winter-is-coming".

You are failing, because your solution returns “winter-is–coming” instead of “winter-is-coming”

2 Likes

this is working for me in a console, passing
"Winter Is Coming" and " Winter Is Coming"
but failing
"A Mind Needs Books Like A Sword Needs A Whetstone" and "Hold The Door"

function urlSlug(title) {
  return [].concat(globalTitle.split(/\W/)).map(item=>item.toLowerCase()).join('-');
}
2 Likes

Thank you for your feedback :slight_smile:

I’ve edited your posts for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

markdown_Forums

This is not functional code. You’re using the global variable globalTitle rather than the parameter title, so the function will always return

[].concat('Winter Is Coming'.split(/\W/)).map(item=>item.toLowerCase()).join('-')

=> 'winter-is-coming'

regardless of input.

Thank you! I was also missing the one with double spaces, but I fixed that too. Thanks again.

I receive

// running test
Your code should not use the replace method for this challenge.
urlSlug(" Winter Is  &nbsp;Coming") should return "winter-is-coming".
// tests completed

I don’t know why $nbsp; is sending a parameter for test and how to remove this space because my solution is

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

// Add your code below this line
function urlSlug(title) {
    //.replace(/&nbsp;/g, ' ')
  return title.toLowerCase().split(" ").join("-");
  
}
// Add your code above this line

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

how to correct my code.

I fixed it by using a Regular Expression /\s+/ instead of " ". This matches one or more spaces. To take the white space off the front I used .trim().

3 Likes

Try this

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

// Add your code below this line
function urlSlug(title) {
title = title.concat();

return title.split(/[,-\s]/g).filter(elem=> elem!==’’).join("-").toLowerCase();

}
// Add your code above this line

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

Try this one.

function urlSlug(title) {
  return title.toLowerCase().split(/\W+/).filter((item)=>item!='').join("-");
  }
2 Likes

Copy that, big chief.

2 Likes

I wonder why /\s*/ wouldn’t work. Any ideas?

I think * matches 0 or more so it would split the string at every character unless there was more than one space in a row.

2 Likes

Try this one.

function urlSlug(title) {
  return title.trim().toLowerCase().split(/\W+/).join("-"); 
}
3 Likes

can anyone tell what’s wrong in my code. it shows correct result in the console but it is not passing the test. Please help

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

// Add your code below this line
function urlSlug(title) {
  return title.toLowerCase().split(/[^a-zA-Z0-9]/).filter((x)=>x!="").join("-");
}
// Add your code above this line

var winterComing = urlSlug(globalTitle); 
console.log(winterComing); //  "winter-is-coming"

You changed the globalTitle var. /The globalTitle variable should not change./

Got the answer here :slight_smile: , could use feedback if theres any shorter way

function urlSlug(title) {
var array = title.trim("")
.split(" “)
.filter(value => value !== “”)
.map(value => value.charAt(0).toLowerCase() + value.slice(1))
.join(”-");

return array;
}

2 Likes

I read carefully like ten times and didn’t see any difference. Is it a joke? I am confused