Why can I not use .replace() here

Or how can I use .replace() funct in this solution?


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

// Only change code below this line
function urlSlug(title) {
return title
.toLowerCase()
.split(" ")
.replace(/^\s+|\s+$/g, "")  // Why I can't use .replace() here?
.join("-");

}
// Only change code above this line
console.log(urlSlug(" 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/83.0.4103.116 Safari/537.36.

Challenge: Apply Functional Programming to Convert Strings to URL Slugs

Link to the challenge:

replace is a string method, split returns an array, you can’t use replace on an array

1 Like

is there anything like .replace() for arrays?

there will be no spaces there, as you have already remvoed all spaces using split as previous method

there are array methods like map or filter that depending on how you use them could do what you need

maybe instead of going to try every possible method, think at the steps you want to take, write them down in plain language, and once you have clear all the steps, then work in converting it to an algorithm

1 Like