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

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

#1

Tell us what’s happening:
Hi everyone,

I am working on Beta exercise. 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.

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/functional-programming/apply-functional-programming-to-convert-strings-to-url-slugs


#2

This one below is the test case I am failing.

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

#3

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


#4

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('-');
}

#5

Thank you for your feedback :slight_smile:


#6

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.


#7

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.


#8

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


#10

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.


#11

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().


#12

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”


#13

Try this one.

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

#14

@sxnaprkhr Your code has been blurred out to avoid spoiling a full working solution for other campers who may not yet want to see a complete solution. In the future, if you post a full passing solution to a challenge and have questions about it, please surround it with [spoiler] and [/spoiler] tags on the line above and below your solution code.

Also, instead of just posting a solution, why not offer the OP suggestions on fixing the code they provided.

Thank you.


#15

Copy that, big chief.


#16

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


#17

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.


#18

Try this one.

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

#19

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"

#20

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


#21

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;
}