Skip to content

Commit 9c04057

Browse files
feat: palindrome algorithm
1 parent 83b05aa commit 9c04057

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export const palindrome = (str: string) => {
2+
const reversed = str.split('').reverse().join('');
3+
4+
return str === reversed;
5+
};
6+
7+
export const palindromeWithEvery = (str: string) => {
8+
return str
9+
.split('')
10+
.every((character, index) => character === str[str.length - index - 1]);
11+
};
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { describe, test } from 'vitest';
2+
import { palindrome, palindromeWithEvery } from '../palindromes';
3+
4+
describe('Palindromes', () => {
5+
test('palindrome function is defined', () => {
6+
expect(typeof palindrome).toEqual('function');
7+
expect(typeof palindromeWithEvery).toEqual('function');
8+
});
9+
10+
test('"aba" is a palindrome', () => {
11+
expect(palindrome('aba')).toBeTruthy();
12+
expect(palindromeWithEvery('aba')).toBeTruthy();
13+
});
14+
15+
test('" aba" is not a palindrome', () => {
16+
expect(palindrome(' aba')).toBeFalsy();
17+
expect(palindromeWithEvery(' aba')).toBeFalsy();
18+
});
19+
20+
test('"aba " is not a palindrome', () => {
21+
expect(palindrome('aba ')).toBeFalsy();
22+
expect(palindromeWithEvery('aba ')).toBeFalsy();
23+
});
24+
25+
test('"greetings" is not a palindrome', () => {
26+
expect(palindrome('greetings')).toBeFalsy();
27+
expect(palindromeWithEvery('greetings')).toBeFalsy();
28+
});
29+
30+
test('"1000000001" a palindrome', () => {
31+
expect(palindrome('1000000001')).toBeTruthy();
32+
expect(palindromeWithEvery('1000000001')).toBeTruthy();
33+
});
34+
35+
test('"Fish hsif" is not a palindrome', () => {
36+
expect(palindrome('Fish hsif')).toBeFalsy();
37+
expect(palindromeWithEvery('Fish hsif')).toBeFalsy();
38+
});
39+
40+
test('"pennep" a palindrome', () => {
41+
expect(palindrome('pennep')).toBeTruthy();
42+
expect(palindromeWithEvery('pennep')).toBeTruthy();
43+
});
44+
});

0 commit comments

Comments
 (0)