2018-10-10 18:03:03 -04:00
---
id: 587d7dab367417b2b2512b6d
title: Apply Functional Programming to Convert Strings to URL Slugs
challengeType: 1
videoUrl: ''
localeTitle: 应用函数式编程将字符串转换为URL Slugs
---
## Description
< section id = "description" > 最后几个挑战涵盖了许多遵循函数式编程原理的有用数组和字符串方法。我们还学习了< code > reduce< / code > ,这是一种用于将问题简化为更简单形式的强大方法。从计算平均值到排序,可以通过应用它来实现任何阵列操作。回想一下< code > map< / code > 和< code > filter< / code > 是< code > reduce< / code > 特例。让我们结合我们学到的东西来解决实际问题。许多内容管理站点( CMS) 将帖子的标题添加到URL的一部分以用于简单的书签目的。例如, 如果您编写一个标题为“Stop Using Reduce”的Medium帖子, 则URL可能会包含某种形式的标题字符串( “... / stop-using-reduce”) 。您可能已经在freeCodeCamp网站上注意到了这一点。 < / section >
## Instructions
< section id = "instructions" > 填写< code > urlSlug< / code > 函数,以便转换字符串< code > title< / code > 并返回URL的连字符版本。您可以使用本节中介绍的任何方法, 也不要使用< code > replace< / code > 。以下是要求:输入是一个带空格和标题字的字符串输出是一个字符串,单词之间的空格用连字符替换( < code > -< / code > )输出应该是所有低位字母输出不应该有任何空格< / section >
## Tests
< section id = 'tests' >
```yml
tests:
- text: < code > globalTitle</ code > 变量不应该更改。
2020-02-18 01:40:55 +09:00
testString: assert(globalTitle === "Winter Is Coming");
2018-10-10 18:03:03 -04:00
- text: 您的代码不应使用< code > replace</ code > 方法来应对此挑战。
2020-02-18 01:40:55 +09:00
testString: assert(!code.match(/\.replace/g));
2018-10-10 18:03:03 -04:00
- text: < code > urlSlug(" Winter Is Coming" )</ code > 应该回归< code > " winter-is-coming" </ code > 。
2020-02-18 01:40:55 +09:00
testString: assert(urlSlug("Winter Is Coming") === "winter-is-coming");
2018-10-10 18:03:03 -04:00
- text: < code > urlSlug(" Winter Is Coming" )</ code > 应该回归< code > " winter-is-coming" </ code > 。
2020-02-18 01:40:55 +09:00
testString: assert(urlSlug(" Winter Is Coming") === "winter-is-coming");
2018-10-10 18:03:03 -04:00
- text: < code > urlSlug(" A Mind Needs Books Like A Sword Needs A Whetstone" )</ code > 应该回归< code > " a-mind-needs-books-like-a-sword-needs-a-whetstone" </ code > 。
2020-02-18 01:40:55 +09:00
testString: assert(urlSlug("A Mind Needs Books Like A Sword Needs A Whetstone") === "a-mind-needs-books-like-a-sword-needs-a-whetstone");
2018-10-10 18:03:03 -04:00
- text: < code > urlSlug(" Hold The Door" )</ code > 应该返回< code > " hold-the-door" </ code > 。
2020-02-18 01:40:55 +09:00
testString: assert(urlSlug("Hold The Door") === "hold-the-door");
2018-10-10 18:03:03 -04:00
```
< / section >
## Challenge Seed
< section id = 'challengeSeed' >
< div id = 'js-seed' >
```js
// the global variable
var globalTitle = "Winter Is Coming";
// Add your code below this line
function urlSlug(title) {
}
// Add your code above this line
var winterComing = urlSlug(globalTitle); // Should be "winter-is-coming"
```
< / div >
< / section >
## Solution
< section id = 'solution' >
```js
// solution required
```
< / section >