Skip to content

Commit c00a311

Browse files
Add "Convert PascalCase string into snake_case" kata
1 parent faa6282 commit c00a311

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<h1>Convert PascalCase string into snake_case <sup><sup>5 Kyu</sup></sup></h1>
2+
3+
<sup>
4+
<a href="https://www.codewars.com/kata/529b418d533b76924600085d">
5+
<strong>LINK TO THE KATA</strong>
6+
</a> - <code>STRINGS</code> <code>ALGORITHMS</code>
7+
</sup>
8+
9+
## Description
10+
11+
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.
12+
13+
**Examples**
14+
15+
```
16+
"TestController" --> "test_controller"
17+
"MoviesAndBooks" --> "movies_and_books"
18+
"App7Test" --> "app7_test"
19+
1 --> "1"
20+
```
21+
22+
## Solution
23+
24+
```javascript
25+
const toUnderscore = string => {
26+
return string
27+
.toString()
28+
.replace(/(.)([A-Z])/g, '$1_$2')
29+
.toLowerCase()
30+
}
31+
```

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ JavaScript katas on Codewars are programming challenges that help you improve yo
3434

3535
- **[By the Power Set of Castle Grayskull](./5-kyu/by-the-power-set-of-castle-grayskull.md)**<br><sup>`MATHEMATICS` `ALGORITHMS`</sup>
3636
- **[Convert A Hex String To RGB](./5-kyu/convert-a-hex-string-to-rgb.md)**<br><sup>`PARSING` `STRINGS` `ALGORITHMS`</sup>
37+
- **[Convert PascalCase string into snake_case](./5-kyu/convert-pascal-case-string-into-snake-case.md)**<br><sup>`STRINGS` `ALGORITHMS`</sup>
3738
- **[Don't Drink the Water](./5-kyu/dont-drink-the-water.md)**<br><sup>`ALGORITHMS` `ARRAYS` `SORTING` `LISTS`</sup>
3839
- **[Extract the domain name from a URL](./5-kyu/extract-the-domain-name-from-a-url.md)**<br><sup>`PARSING` `REGULAR EXPRESSIONS`</sup>
3940
- **[First non-repeating character](./5-kyu/first-non-repeating-character.md)**<br><sup>`STRINGS` `ALGORITHMS`</sup>

0 commit comments

Comments
 (0)