Apply Functional Programming to Convert Strings to URL Slugs prb

Apply Functional Programming to Convert Strings to URL Slugs prb
0

#1

Tell us what’s happening:

I can’t find the problem in my code. Someone, please help me out.

Your code so far


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

// Add your code below this line
function urlSlug(title) {
  let original= title.toLowerCase();
  let word = original.split(" ");
  let result =word.join("-");
  return result;
  
}
// 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 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36.

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


#2

In the test case of urlSlug(" Winter Is Coming"), your function returns “-winter-is–coming” instead of the correct answer of “winter-is-coming”

Inside the function, you assign [ ‘’, ‘winter’, ‘is’, ‘’, ‘coming’ ] to word with split(" “). Then when you write word.join(”-") it puts a “-” between each element. Basically it creates a string similar to how the following code concatenates the array together:

let result = '' + '-' + 'winter' + '-' + 'is' + '-' + '' + '-' + 'coming';

#3

so what kind of coding should i use?


#4

There are many ways to solve this challenge.

Your existing solution of splitting the string into words would work as long as there is only a single space character between words and there was no space at the beginning or ending parts.

I will give you one hint:

  1. Look at the String.prototype.trim method to deal with spaces on either end of the string.