* fix: move "Explore Differences Between..." to basic JS, update seed and tests * fix: resequence "Declare String Variables" * fix: move "Declare a Read-Only Variable..." to basic JS, update seed and tests * fix: revert changes to non-English "Explore Differences Between..." test text * fix: revert test strings, solutions, and seeds for non-English challenges * fix: update "Declare String Variables" description * fix: sync quotation marks in description and seed * fix: modify note in "Declare a Read-Only..." challenge * fix: update operator and compound assignment challenges * fix: update string challenges * fix: update array and array method challenges * fix: update function and scope challenges, resequence slightly * fix: "Word Blanks" solution * fix: add spacing to seed * fix: concatenating += challenge spacing * fix: appending variables to strings spacing * fix: find the length of a string spacing * fix: removed instances of removedFromMyArray = 0 * fix: switch challenges * fix: function argument and param spacing * fix: update counting cards, object challenges, and record collection * fix: finish rest of Basic JS section * fix: introducing else statements solution * fix: update spacing and wording * fix: update wording for const challenge * fix: update functional programming challenges * fix: intermediate algorithms and cert challenges * fix: revert some spacing and remove comments for fp challenge solutions * feat: add notes with links to moved let and const challenges in first two es6 challenges * fix: update es6 intro text * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/concatenating-strings-with-the-plus-equals-operator.md Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/finding-a-remainder-in-javascript.md Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/global-scope-and-functions.md Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/iterate-through-an-array-with-a-for-loop.md Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/iterate-through-an-array-with-a-for-loop.md Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/functional-programming/implement-map-on-a-prototype.md Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * Update curriculum/challenges/english/02-javascript-algorithms-and-data-structures/basic-javascript/declare-a-read-only-variable-with-the-const-keyword.md Co-authored-by: Shaun Hamilton <shauhami020@gmail.com> * fix: concatenating strings with plus operator seed * fix: add comments back to Declare a Read-Only Variable... seed * feat: add es6 to basic javascript redirect tests for let and const challenges * fix: revert "Concatenating Strings with Plus Operator" seed * fix: move test file to cypress/integration/learn/redirects, separate redirect tests Co-authored-by: Shaun Hamilton <shauhami020@gmail.com>
84 lines
2.5 KiB
Markdown
84 lines
2.5 KiB
Markdown
---
|
|
id: 587d7dab367417b2b2512b6d
|
|
title: Apply Functional Programming to Convert Strings to URL Slugs
|
|
challengeType: 1
|
|
forumTopicId: 301227
|
|
dashedName: apply-functional-programming-to-convert-strings-to-url-slugs
|
|
---
|
|
|
|
# --description--
|
|
|
|
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`.
|
|
|
|
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.
|
|
|
|
# --instructions--
|
|
|
|
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:
|
|
|
|
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
|
|
|
|
# --hints--
|
|
|
|
Your code should not use the `replace` method for this challenge.
|
|
|
|
```js
|
|
assert(!code.match(/\.?[\s\S]*?replace/g));
|
|
```
|
|
|
|
`urlSlug("Winter Is Coming")` should return the string `winter-is-coming`.
|
|
|
|
```js
|
|
assert(urlSlug('Winter Is Coming') === 'winter-is-coming');
|
|
```
|
|
|
|
`urlSlug(" Winter Is Coming")` should return the string `winter-is-coming`.
|
|
|
|
```js
|
|
assert(urlSlug(' Winter Is Coming') === 'winter-is-coming');
|
|
```
|
|
|
|
`urlSlug("A Mind Needs Books Like A Sword Needs A Whetstone")` should return the string `a-mind-needs-books-like-a-sword-needs-a-whetstone`.
|
|
|
|
```js
|
|
assert(
|
|
urlSlug('A Mind Needs Books Like A Sword Needs A Whetstone') ===
|
|
'a-mind-needs-books-like-a-sword-needs-a-whetstone'
|
|
);
|
|
```
|
|
|
|
`urlSlug("Hold The Door")` should return the string `hold-the-door`.
|
|
|
|
```js
|
|
assert(urlSlug('Hold The Door') === 'hold-the-door');
|
|
```
|
|
|
|
# --seed--
|
|
|
|
## --seed-contents--
|
|
|
|
```js
|
|
// Only change code below this line
|
|
function urlSlug(title) {
|
|
|
|
|
|
}
|
|
// Only change code above this line
|
|
```
|
|
|
|
# --solutions--
|
|
|
|
```js
|
|
function urlSlug(title) {
|
|
return title.trim().split(/\s+/).join("-").toLowerCase();
|
|
}
|
|
```
|