Apply Functional Programming to Convert Strings to URL Slugs prb

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:

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

so what kind of coding should i use?

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.