Skip to content

Commit 6453751

Browse files
authored
String cleaning in JS
1 parent c55a12c commit 6453751

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// 1 Plain solution
2+
function stringClean(s) {
3+
let res = '';
4+
5+
for (let i = 0; i < s.length; i++) {
6+
if (s.charCodeAt(i) < 48 || s.charCodeAt(i) > 57) {
7+
res += s[i];
8+
}
9+
}
10+
11+
return res;
12+
}
13+
14+
// 2 Optimized solution
15+
function stringClean(s) {
16+
return [...s]
17+
.filter(
18+
c => c.charCodeAt(0) < 48
19+
|| c.charCodeAt(0) > 57
20+
)
21+
.join('');
22+
}
23+
24+
// 3 Clever solution
25+
const stringClean = s => s.replace(/\d/g, "");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# String cleaning (8 kyu)
2+
3+
https://www.codewars.com/kata/57e1e61ba396b3727c000251
4+
5+
Your boss decided to save money by purchasing some cut-rate optical character recognition software for scanning in the text of old novels to your database. At first it seems to capture words okay, but you quickly notice that it throws in a lot of numbers at random places in the text.
6+
7+
## Examples (input -> output)
8+
9+
```
10+
'! !' -> '! !'
11+
'123456789' -> ''
12+
'This looks5 grea8t!' -> 'This looks great!'
13+
```
14+
15+
Your harried co-workers are looking to you for a solution to take this garbled text and remove all of the numbers. Your program will take in a string and clean out all numeric characters, and return a string with spacing and special characters `~#$%^&!@*():;"'.,?` all intact.

README.md

+11
Original file line numberDiff line numberDiff line change
@@ -1102,6 +1102,17 @@ JS Video Tutorial) :
11021102
/ [[Java](<8kyu/(8%20kyu)%20Square(n)%20Sum/(8%20kyu)%20Square(n)%20Sum.java>)]
11031103
/ [[Go](<8kyu/(8%20kyu)%20Square(n)%20Sum/(8%20kyu)%20Square(n)%20Sum.go>)]
11041104
<br>
1105+
**[(8 kyu) String cleaning](https://www.codewars.com/kata/57e1e61ba396b3727c000251/)** ([Description](<8kyu/(8%20kyu)%20String%20cleaning/(8%20kyu)%20String%20cleaning.md>) +
1106+
JS Video Tutorial) :
1107+
[[JavaScript](<8kyu/(8%20kyu)%20String%20cleaning/(8%20kyu)%20String%20cleaning.js>)]
1108+
/ [TypeScript]
1109+
/ [Python]
1110+
/ [Rust]
1111+
/ [C++]
1112+
/ [C#]
1113+
/ [Java]
1114+
/ [Go]
1115+
<br>
11051116
**[(8 kyu) String repeat](https://www.codewars.com/kata/string-repeat)**
11061117
([Description](<8kyu/(8%20kyu)%20String%20repeat/(8%20kyu)%20String%20repeat.md>) +
11071118
[JS Video Tutorial](https://www.youtube.com/watch?v=SN6hqiExBgs)) :

0 commit comments

Comments
 (0)