Skip to content

Instantly share code, notes, and snippets.

@MrSkinny
Created November 28, 2015 00:17
Show Gist options
  • Save MrSkinny/9fe2d67eb62824109a6b to your computer and use it in GitHub Desktop.
Save MrSkinny/9fe2d67eb62824109a6b to your computer and use it in GitHub Desktop.
FCC: Intermediate Algorithm Scripting: Bonfire 12: Spinal Tap Case
// Bonfire: Spinal Tap Case
// Author: @mrskinny
// Challenge: http://www.freecodecamp.com/challenges/bonfire-spinal-tap-case?solution=function%20spinalCase(str)%20%7B%0A%20%20%2F%2F%20%22It%27s%20such%20a%20fine%20line%20between%20stupid%2C%20and%20clever.%22%0A%20%20%2F%2F%20--David%20St.%20Hubbins%0A%20%20%0A%20%20if%20(%20!str.match(%2F%5Cs%2F)%20%26%26%20!str.match(%2F%5C_%2F)%20)%20%7B%0A%20%20%20%20var%20newLetters%20%3D%20%5B%5D%3B%0A%20%20%20%20letters%20%3D%20str.split(%27%27)%3B%0A%0A%20%20%20%20for%20(%20var%20i%20%3D%200%2C%20_len%20%3D%20letters.length%3B%20i%20%3C%20_len%3B%20i%2B%2B)%7B%0A%20%20%20%20%20%20if%20(letters%5Bi%5D.toUpperCase()%20%3D%3D%3D%20letters%5Bi%5D)%20newLetters.push(%27-%27)%3B%0A%20%20%20%20%20%20newLetters.push(letters%5Bi%5D)%3B%0A%20%20%20%20%7D%0A%20%20%20%20str%20%3D%20newLetters.join(%27%27)%3B%0A%20%20%7D%0A%20%20%0A%20%20str%20%3D%20str.toLowerCase()%3B%0A%0A%20%20str%20%3D%20str.replace(%2F%5Cs%2Fg%2C%20%27-%27)%3B%0A%20%20str%20%3D%20str.replace(%2F%5C_%2Fg%2C%20%27-%27)%3B%0A%20%20%0A%20%20return%20str%3B%0A%7D%0A%0AspinalCase(%27thisIsSpinalTap%27)%3B%0A
// Learn to Code at Free Code Camp (www.freecodecamp.com)
function spinalCase(str) {
// "It's such a fine line between stupid, and clever."
// --David St. Hubbins
if ( !str.match(/\s/) && !str.match(/\_/) ) {
var newLetters = [];
letters = str.split('');
for ( var i = 0, _len = letters.length; i < _len; i++){
if (letters[i].toUpperCase() === letters[i]) newLetters.push('-');
newLetters.push(letters[i]);
}
str = newLetters.join('');
}
str = str.toLowerCase();
str = str.replace(/\s/g, '-');
str = str.replace(/\_/g, '-');
return str;
}
spinalCase('thisIsSpinalTap');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment