Skip to content

Instantly share code, notes, and snippets.

@jluismm2311
Created December 1, 2015 15:40
Show Gist options
  • Save jluismm2311/9e326855f4f8626f05d5 to your computer and use it in GitHub Desktop.
Save jluismm2311/9e326855f4f8626f05d5 to your computer and use it in GitHub Desktop.
Complete the function/method so that it takes CamelCase string and returns the string in snake_case notation. Lowercase characters can be numbers. If method gets number, it should return string. Examples: # returns test_controller to_underscore('TestController') # returns movies_and_books to_underscore('MoviesAndBooks') # returns app7_test to_un…
function toUnderscore(string) {
return string.toString().replace(/[A-Z]/g,function(element, index){
element = element.toLowerCase();
return index == 0 ? element :'_'+ element
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment