2018-10-12 15:37:13 -04:00
---
title: Spinal Tap Case
---
2019-07-24 00:59:27 -07:00
# Spinal Tap Case
---
## Problem Explanation
2018-10-12 15:37:13 -04:00
Convert the given string to a lowercase sentence with words joined by dashes.
#### Relevant Links
* < a href = 'https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String' target = '_blank' rel = 'nofollow' > String global object</ a >
2019-04-24 10:19:44 -04:00
* < a href = 'https://guide.freecodecamp.org/javascript/regular-expressions-reference/' target = '_blank' rel = 'nofollow' > JS Regex Resources</ a >
2018-10-12 15:37:13 -04:00
* < a href = 'http://forum.freecodecamp.com/t/javascript-string-prototype-replace/15942' target = '_blank' rel = 'nofollow' > JS String Prototype Replace</ a >
* < a href = 'http://forum.freecodecamp.com/t/javascript-string-prototype-tolowercase/15948' target = '_blank' rel = 'nofollow' > JS String Prototype ToLowerCase</ a >
2019-07-24 00:59:27 -07:00
---
## Hints
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
### Hint 1
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
Create a regular expression for all white spaces and underscores.
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
### Hint 2
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
You will also have to make everything lowercase.
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
### Hint 3
2018-10-12 15:37:13 -04:00
The tricky part is getting the regular expression part to work, once you do that then just turn the uppercase to lowercase and replace spaces with underscores using `replace()` .
2019-07-24 00:59:27 -07:00
---
## Solutions
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
< details > < summary > Solution 1 (Click to Show/Hide)< / summary >
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
```javascript
function spinalCase(str) {
// Create a variable for the white space and underscores.
var regex = /\s+|_+/g;
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
// Replace low-upper case to low-space-uppercase
str = str.replace(/([a-z])([A-Z])/g, "$1 $2");
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
// Replace space and underscore with -
return str.replace(regex, "-").toLowerCase();
}
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
// test here
spinalCase("This Is Spinal Tap");
```
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
#### Code Explanation
2018-10-12 15:37:13 -04:00
* **regex** contains the regular expression `/\s+|_+/g` , which will select all white spaces and underscores.
* The first `replace()` puts a space before any encountered uppercase characters in the string **str** so that the spaces can be replaced by dashes later on.
* While returning the string, another `replace()` replaces spaces and underscores with dashes using **regex** .
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
2019-07-24 00:59:27 -07:00
```javascript
function spinalCase(str) {
// Replace low-upper case to low-space-uppercase
str = str.replace(/([a-z])([A-Z])/g, "$1 $2");
// Split on whitespace and underscores and join with dash
return str
.toLowerCase()
.split(/(?:_| )+/)
.join("-");
}
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
// test here
spinalCase("This Is Spinal Tap");
```
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
#### Code Explanation
2018-10-12 15:37:13 -04:00
* Similar to the first solution, the first `replace()` puts a space before any encountered uppercase characters in the string **str** so that the spaces can be replaced by dashes later on.
* Instead of using `replace()` here to replace whitespace and underscores with dashes, the string is `split()` on the regular expression `/(?:_| )+/` and `join()` -ed on `-` .
#### Relevant Links
* < a href = 'http://forum.freecodecamp.com/t/javascript-string-prototype-split/15944' target = '_blank' rel = 'nofollow' > JS String Prototype Split</ a >
* < a href = 'http://forum.freecodecamp.com/t/javascript-array-prototype-join/14292' target = '_blank' rel = 'nofollow' > JS Array Prototype Join</ a >
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 3 (Click to Show/Hide)< / summary >
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
```javascript
function spinalCase(str) {
// "It's such a fine line between stupid, and clever."
// --David St. Hubbins
2018-10-12 15:37:13 -04:00
2019-07-24 00:59:27 -07:00
return str
.split(/\s|_|(?=[A-Z])/)
.join("-")
.toLowerCase();
}
```
#### Code Explanation
2018-10-12 15:37:13 -04:00
* Split the string at one of the following conditions (_converted to an array_)
* a whitespace character [`\s` ] is encountered
* underscore character [`_` ] is encountered
* or is followed by an uppercase letter [`(?=[A-Z])` ]
* Join the array using a hyphen (`-` )
* Lowercase the whole resulting string
#### Relevant Links
* < a href = 'http://devdocs.io/javascript/global_objects/string/split' target = '_blank' rel = 'nofollow' > String#split </ a >
* < a href = 'http://devdocs.io/javascript/global_objects/regexp' target = '_blank' rel = 'nofollow' > RegExp</ a >
* < a href = 'http://devdocs.io/javascript/global_objects/array/join' target = '_blank' rel = 'nofollow' > Arrray#join </ a >
* < a href = 'http://devdocs.io/javascript/global_objects/string/tolowercase' target = '_blank' rel = 'nofollow' > String#toLowerCase </ a >
2019-07-24 00:59:27 -07:00
< / details >