Skip to content

Commit

Permalink
feat: palindrome algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
francislagares committed Aug 12, 2023
1 parent 83b05aa commit 9c04057
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/algorithms/palindromes/palindromes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const palindrome = (str: string) => {
const reversed = str.split('').reverse().join('');

return str === reversed;
};

export const palindromeWithEvery = (str: string) => {
return str
.split('')
.every((character, index) => character === str[str.length - index - 1]);
};
44 changes: 44 additions & 0 deletions src/algorithms/palindromes/tests/palindromes.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { describe, test } from 'vitest';
import { palindrome, palindromeWithEvery } from '../palindromes';

describe('Palindromes', () => {
test('palindrome function is defined', () => {
expect(typeof palindrome).toEqual('function');
expect(typeof palindromeWithEvery).toEqual('function');
});

test('"aba" is a palindrome', () => {
expect(palindrome('aba')).toBeTruthy();
expect(palindromeWithEvery('aba')).toBeTruthy();
});

test('" aba" is not a palindrome', () => {
expect(palindrome(' aba')).toBeFalsy();
expect(palindromeWithEvery(' aba')).toBeFalsy();
});

test('"aba " is not a palindrome', () => {
expect(palindrome('aba ')).toBeFalsy();
expect(palindromeWithEvery('aba ')).toBeFalsy();
});

test('"greetings" is not a palindrome', () => {
expect(palindrome('greetings')).toBeFalsy();
expect(palindromeWithEvery('greetings')).toBeFalsy();
});

test('"1000000001" a palindrome', () => {
expect(palindrome('1000000001')).toBeTruthy();
expect(palindromeWithEvery('1000000001')).toBeTruthy();
});

test('"Fish hsif" is not a palindrome', () => {
expect(palindrome('Fish hsif')).toBeFalsy();
expect(palindromeWithEvery('Fish hsif')).toBeFalsy();
});

test('"pennep" a palindrome', () => {
expect(palindrome('pennep')).toBeTruthy();
expect(palindromeWithEvery('pennep')).toBeTruthy();
});
});

0 comments on commit 9c04057

Please sign in to comment.