forked from freeCodeCamp/freeCodeCamp
-
Notifications
You must be signed in to change notification settings - Fork 1
Algorithm Spinal Tap Case
Rafael J. Rodriguez edited this page Jun 13, 2016
·
7 revisions
- Convert the given string to an all lowercase sentence joined by dashes.
- Create a regex to for all white spaces and underscores.
- You will also have to make everything lowercase.
- 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()
Solution ahead!
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();
}
// test here
spinalCase('This Is Spinal Tap');🚀 Run Code
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('-');
}
// test here
spinalCase('This Is Spinal Tap');🚀 Run Code
- Read comments in code.
If you found this page useful, you can give thanks by copying and pasting this on the main chat: Thanks @Rafase282 @anuragaryan for your help with Algorithm: Spinal Tap Case
NOTE: Please add your username only if you have added any relevant main contents to the wiki page. (Please don't remove any existing usernames.)
Learn to code and help nonprofits. Join our open source community in 15 seconds at http://freecodecamp.com
Follow our Medium blog
Follow Quincy on Quora
Follow us on Twitter
Like us on Facebook
And be sure to click the "Star" button in the upper right of this page.
New to Free Code Camp?
JS Concepts
JS Language Reference
- arguments
- Array.prototype.filter
- Array.prototype.indexOf
- Array.prototype.map
- Array.prototype.pop
- Array.prototype.push
- Array.prototype.shift
- Array.prototype.slice
- Array.prototype.some
- Array.prototype.toString
- Boolean
- for loop
- for..in loop
- for..of loop
- String.prototype.split
- String.prototype.toLowerCase
- String.prototype.toUpperCase
- undefined
Other Links