This content originally appeared on DEV Community and was authored by Randy Rivera
Alright now with everything we've learned so far, let's apply to another 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)
. - Let's Fill in the
urlSlug
function so it converts a string title and returns the hyphenated version for the URL. - 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
// Only change code below this line
function urlSlug(title) {
}
// Only change code above this line
- Answer:
function urlSlug(title) {
return title
.toLowerCase()
.split(" ")
.filter(str => str != "") // <-- basically saying if the str being passed is not equal to an empty string return that str
.join("-")
}
console.log(urlSlug(" Winter Is Coming"))
-
urlSlug(" Winter Is Coming")
returns the stringwinter-is-coming
This content originally appeared on DEV Community and was authored by Randy Rivera
Print
Share
Comment
Cite
Upload
Translate
Updates
There are no updates yet.
Click the Upload button above to add an update.

APA
MLA
Randy Rivera | Sciencx (2021-07-04T15:00:02+00:00) Apply Functional Programming to Convert Strings to URL Slugs. Retrieved from https://www.scien.cx/2021/07/04/apply-functional-programming-to-convert-strings-to-url-slugs/
" » Apply Functional Programming to Convert Strings to URL Slugs." Randy Rivera | Sciencx - Sunday July 4, 2021, https://www.scien.cx/2021/07/04/apply-functional-programming-to-convert-strings-to-url-slugs/
HARVARDRandy Rivera | Sciencx Sunday July 4, 2021 » Apply Functional Programming to Convert Strings to URL Slugs., viewed ,<https://www.scien.cx/2021/07/04/apply-functional-programming-to-convert-strings-to-url-slugs/>
VANCOUVERRandy Rivera | Sciencx - » Apply Functional Programming to Convert Strings to URL Slugs. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/07/04/apply-functional-programming-to-convert-strings-to-url-slugs/
CHICAGO" » Apply Functional Programming to Convert Strings to URL Slugs." Randy Rivera | Sciencx - Accessed . https://www.scien.cx/2021/07/04/apply-functional-programming-to-convert-strings-to-url-slugs/
IEEE" » Apply Functional Programming to Convert Strings to URL Slugs." Randy Rivera | Sciencx [Online]. Available: https://www.scien.cx/2021/07/04/apply-functional-programming-to-convert-strings-to-url-slugs/. [Accessed: ]
rf:citation » Apply Functional Programming to Convert Strings to URL Slugs | Randy Rivera | Sciencx | https://www.scien.cx/2021/07/04/apply-functional-programming-to-convert-strings-to-url-slugs/ |
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.