reduce
, which is a powerful method used to reduce problems to simpler forms. From computing averages to sorting, any array operation can be achieved by applying it. Recall that map
and filter
are special cases of reduce
.
Let's combine what we've learned to solve a practical problem.
Many content management sites (CMS) have the titles of a post added to part of the URL for simple bookmarking purposes. For example, if you write a Medium post titled "Stop Using Reduce", it's likely the URL would have some form of the title string in it (".../stop-using-reduce"). You may have already noticed this on the freeCodeCamp site.
urlSlug
function so it converts a string title
and returns the hyphenated version for the URL. You can use any of the methods covered in this section, and don't use replace
. Here are the requirements:
The input is a string with spaces and title-cased words
The output is a string with the spaces between words replaced by a hyphen (-
)
The output should be all lower-cased letters
The output should not have any spaces
globalTitle
variable should not change.
testString: assert(globalTitle === "Winter Is Coming");
- text: Your code should not use the replace
method for this challenge.
testString: assert(!code.match(/\.?[\s\S]*?replace/g));
- text: urlSlug("Winter Is Coming")
should return "winter-is-coming"
.
testString: assert(urlSlug("Winter Is Coming") === "winter-is-coming");
- text: urlSlug(" Winter Is Coming")
should return "winter-is-coming"
.
testString: assert(urlSlug(" Winter Is Coming") === "winter-is-coming");
- text: urlSlug("A Mind Needs Books Like A Sword Needs A Whetstone")
should return "a-mind-needs-books-like-a-sword-needs-a-whetstone"
.
testString: assert(urlSlug("A Mind Needs Books Like A Sword Needs A Whetstone") === "a-mind-needs-books-like-a-sword-needs-a-whetstone");
- text: urlSlug("Hold The Door")
should return "hold-the-door"
.
testString: assert(urlSlug("Hold The Door") === "hold-the-door");
```