Skip to content

Commit 83b05aa

Browse files
feat: string reversal algorithm
1 parent eb6853e commit 83b05aa

File tree

27 files changed

+52
-8
lines changed

27 files changed

+52
-8
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export const reverseString = (text: string) => {
2+
return Array.from(text).reverse().join('');
3+
};
4+
5+
export const reverseWithLoop = (text: string) => {
6+
let reversed = '';
7+
8+
for (const character of text) {
9+
reversed = character + reversed;
10+
}
11+
12+
return reversed;
13+
};
14+
15+
export const reverseWithReduce = (text: string) => {
16+
return text
17+
.split('')
18+
.reduce((reversed, character) => character + reversed, '');
19+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { describe, test } from 'vitest';
2+
import {
3+
reverseString,
4+
reverseWithLoop,
5+
reverseWithReduce,
6+
} from '../string-reverse';
7+
8+
describe('String Reversal', () => {
9+
test('Reverse function exists', () => {
10+
expect([reverseString, reverseWithLoop, reverseWithReduce]).toBeDefined();
11+
});
12+
13+
test('Reverse reverses a string', () => {
14+
expect(reverseString('abcd')).toEqual('dcba');
15+
expect(reverseWithLoop('abcd')).toEqual('dcba');
16+
expect(reverseWithReduce('abcd')).toEqual('dcba');
17+
});
18+
19+
test('Reverse reverses a string', () => {
20+
expect(reverseString(' abcd')).toEqual('dcba ');
21+
expect(reverseWithLoop(' abcd')).toEqual('dcba ');
22+
expect(reverseWithReduce(' abcd')).toEqual('dcba ');
23+
});
24+
});
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)