Bonfire Spinal Tap Case - GJSmith3rd/FreeCodeCamp-BootCamp GitHub Wiki
Contact me
Gilbert Joseph Smith III
Github | FreeCodeCamp | CodePen | LinkedIn | Blog/Site | E-Mail
Details
- Difficulty: 2/5
Convert a string to spinal case. Spinal case is all-lowercase-words-joined-by-dashes.
Remember to use Read-Search-Ask if you get stuck. Try to pair program. Write your own code.
Useful Links
Problem Script:
function spinalCase(str) {
// "It's such a fine line between stupid, and clever."
// --David St. Hubbins
return str;
}
spinalCase('This Is Spinal Tap');
Problem Explanation:
- Convert the given string to an all lowercase sentence joined by dashes.
Hint: 1
- Create a regex to for all white spaces and underscores.
Hint: 2
- You will also have to make everything lowercase.
Hint: 3
- The tricky part is getting the regex part to work, once you do that then just turn the uppercase to lowercase and replace spaces with underscores using
replace()
Spoiler Alert!
Solution ahead!
Code Solution:
function spinalCase(str) {
// Create a variable for the white space and underscores.
var regex = /\s+|_+/g;
// Replace low-upper case to low-space-uppercase
str = str.replace(/([a-z])([A-Z])/g, '$1 $2');
// Replace space and underscore with -
return str.replace(regex, '-').toLowerCase();
}
Code Explanation:
- Read comments in code.