Skip to content

Commit 49522ad

Browse files
feat: capitalization algorithm
1 parent 8c89a95 commit 49522ad

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Option 1
2+
export const capitalize = (str: string) => {
3+
const words = [];
4+
5+
for (const word of str.split(' ')) {
6+
words.push(word[0].toUpperCase() + word.slice(1));
7+
}
8+
9+
return words.join(' ');
10+
};
11+
12+
// Option 2
13+
export const capitalizeString = (str: string) => {
14+
let result = str[0].toUpperCase();
15+
16+
for (let i = 1; i < str.length; i++) {
17+
if (str[i - 1] === ' ') {
18+
result += str[i].toUpperCase();
19+
} else {
20+
result += str[i];
21+
}
22+
}
23+
24+
return result;
25+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { capitalize, capitalizeString } from '../capitalize';
2+
3+
describe('Capitalize Algorithm', () => {
4+
test('Capitalize is a function', () => {
5+
expect(typeof capitalize).toEqual('function');
6+
expect(typeof capitalizeString).toEqual('function');
7+
});
8+
9+
test('capitalizes the first letter of every word', () => {
10+
expect(capitalize('hi there, how is it going?')).toEqual(
11+
'Hi There, How Is It Going?',
12+
);
13+
expect(capitalizeString('hi there, how is it going?')).toEqual(
14+
'Hi There, How Is It Going?',
15+
);
16+
});
17+
});

0 commit comments

Comments
 (0)