2018-10-10 18:03:03 -04:00
---
id: 587d7dab367417b2b2512b6d
2021-02-06 04:42:36 +00:00
title: Apply Functional Programming to Convert Strings to URL Slugs
2018-10-10 18:03:03 -04:00
challengeType: 1
2020-08-05 16:38:04 +08:00
forumTopicId: 301227
2021-01-13 03:31:00 +01:00
dashedName: apply-functional-programming-to-convert-strings-to-url-slugs
2018-10-10 18:03:03 -04:00
---
2020-12-16 00:37:30 -07:00
# --description--
2021-02-06 04:42:36 +00:00
The last several challenges covered a number of useful array and string methods that follow functional programming principles. We've also learned about `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` .
2020-12-16 00:37:30 -07:00
2021-02-06 04:42:36 +00:00
Let's combine what we've learned to solve a practical problem.
2020-12-16 00:37:30 -07:00
2021-02-06 04:42:36 +00:00
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.
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
# --instructions--
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
Fill in the `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:
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
The input is a string with spaces and title-cased words
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
The output is a string with the spaces between words replaced by a hyphen (`-` )
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
The output should be all lower-cased letters
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
The output should not have any spaces
2020-12-16 00:37:30 -07:00
# --hints--
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
Your code should not use the `replace` method for this challenge.
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
2021-02-06 04:42:36 +00:00
assert(!code.match(/\.?[\s\S]*?replace/g));
2020-12-16 00:37:30 -07:00
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
`urlSlug("Winter Is Coming")` should return `"winter-is-coming"` .
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(urlSlug('Winter Is Coming') === 'winter-is-coming');
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
`urlSlug(" Winter Is Coming")` should return `"winter-is-coming"` .
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(urlSlug(' Winter Is Coming') === 'winter-is-coming');
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
`urlSlug("A Mind Needs Books Like A Sword Needs A Whetstone")` should return `"a-mind-needs-books-like-a-sword-needs-a-whetstone"` .
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(
urlSlug('A Mind Needs Books Like A Sword Needs A Whetstone') ===
'a-mind-needs-books-like-a-sword-needs-a-whetstone'
);
```
2020-08-05 16:38:04 +08:00
2021-02-06 04:42:36 +00:00
`urlSlug("Hold The Door")` should return `"hold-the-door"` .
2020-08-05 16:38:04 +08:00
2020-12-16 00:37:30 -07:00
```js
assert(urlSlug('Hold The Door') === 'hold-the-door');
2018-10-10 18:03:03 -04:00
```
2020-08-05 16:38:04 +08:00
2021-01-13 03:31:00 +01:00
# --seed--
## --seed-contents--
```js
// Only change code below this line
function urlSlug(title) {
}
// Only change code above this line
```
2020-12-16 00:37:30 -07:00
# --solutions--
2021-01-13 03:31:00 +01:00
```js
// Only change code below this line
function urlSlug(title) {
return title.trim().split(/\s+/).join("-").toLowerCase();
}
```