2018-10-12 15:37:13 -04:00
|
|
|
---
|
|
|
|
title: Apply Functional Programming to Convert Strings to URL Slugs
|
|
|
|
---
|
2019-07-24 00:59:27 -07:00
|
|
|
# Apply Functional Programming to Convert Strings to URL Slugs
|
2018-10-12 15:37:13 -04:00
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
|
|
|
|
---
|
|
|
|
## Solutions
|
|
|
|
|
|
|
|
<details><summary>Solution 1 (Click to Show/Hide)</summary>
|
2018-10-12 15:37:13 -04:00
|
|
|
|
|
|
|
```javascript
|
|
|
|
// the global variable
|
|
|
|
var globalTitle = "Winter Is Coming";
|
|
|
|
|
|
|
|
// Add your code below this line
|
|
|
|
function urlSlug(title) {
|
2019-07-24 00:59:27 -07:00
|
|
|
return title
|
|
|
|
.split(/\W/)
|
|
|
|
.filter(obj => {
|
|
|
|
return obj !== "";
|
|
|
|
})
|
|
|
|
.join("-")
|
|
|
|
.toLowerCase();
|
2018-10-12 15:37:13 -04:00
|
|
|
}
|
|
|
|
// Add your code above this line
|
|
|
|
|
|
|
|
var winterComing = urlSlug(globalTitle); // Should be "winter-is-coming"
|
|
|
|
```
|
2019-07-24 00:59:27 -07:00
|
|
|
</details>
|
2018-10-12 15:37:13 -04:00
|
|
|
|
2019-07-24 00:59:27 -07:00
|
|
|
<details><summary>Solution 2 (Click to Show/Hide)</summary>
|
2018-10-12 15:37:13 -04:00
|
|
|
|
|
|
|
```javascript
|
|
|
|
// the global variable
|
|
|
|
var globalTitle = "Winter Is Coming";
|
|
|
|
|
|
|
|
// Add your code below this line
|
|
|
|
function urlSlug(title) {
|
2019-07-24 00:59:27 -07:00
|
|
|
return title
|
|
|
|
.toLowerCase()
|
|
|
|
.trim()
|
|
|
|
.split(/\s+/)
|
|
|
|
.join("-");
|
2018-10-12 15:37:13 -04:00
|
|
|
}
|
|
|
|
// Add your code above this line
|
|
|
|
|
|
|
|
var winterComing = urlSlug(globalTitle); // Should be "winter-is-coming"
|
|
|
|
```
|
2019-07-24 00:59:27 -07:00
|
|
|
|
|
|
|
</details>
|