Skip to content

Latest commit

 

History

History
31 lines (24 loc) · 812 Bytes

convert-pascal-case-string-into-snake-case.md

File metadata and controls

31 lines (24 loc) · 812 Bytes

Convert PascalCase string into snake_case 5 Kyu

LINK TO THE KATA - STRINGS ALGORITHMS

Description

Complete the function/method so that it takes a PascalCase string and returns the string in snake_case notation. Lowercase characters can be numbers. If the method gets a number as input, it should return a string.

Examples

"TestController"  -->  "test_controller"
"MoviesAndBooks"  -->  "movies_and_books"
"App7Test"        -->  "app7_test"
1                 -->  "1"

Solution

const toUnderscore = string => {
  return string
    .toString()
    .replace(/(.)([A-Z])/g, '$1_$2')
    .toLowerCase()
}