Skip to content

Commit 952d5ed

Browse files
feat: add palindrome check algorithm
1 parent 5c39e87 commit 952d5ed

2 files changed

Lines changed: 48 additions & 21 deletions

File tree

‎String/CheckPalindrome.js‎

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1-
// Palindrome check is case sensitive; i.e., Aba is not a palindrome
2-
// input is a string
1+
/**
2+
* @function checkPalindrome
3+
* @description A palindrome is a word, phrase, or sequence that reads the
4+
* same backwards as forwards. This function checks if a given string
5+
* is a palindrome, ignoring case and non-alphanumeric characters.
6+
* @param {string} str - The input string to check
7+
* @returns {boolean} - True if the string is a palindrome, false otherwise
8+
* @see https://en.wikipedia.org/wiki/Palindrome
9+
* @example checkPalindrome("racecar") => true
10+
* @example checkPalindrome("hello") => false
11+
* @example checkPalindrome("A man a plan a canal Panama") => true
12+
*/
313
const checkPalindrome = (str) => {
4-
// check that input is a string
514
if (typeof str !== 'string') {
6-
return 'Not a string'
15+
throw new TypeError('Input must be a string')
716
}
8-
if (str.length === 0) {
9-
return 'Empty string'
10-
}
11-
// Reverse only works with array, thus convert the string to array, reverse it and convert back to string
12-
// return as palindrome if the reversed string is equal to the input string
13-
const reversed = [...str].reverse().join('')
14-
return str === reversed ? 'Palindrome' : 'Not a Palindrome'
17+
const cleaned = str.toLowerCase().replace(/[^a-z0-9]/g, '')
18+
return cleaned === cleaned.split('').reverse().join('')
1519
}
1620

1721
export { checkPalindrome }
Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,39 @@
1-
import { checkPalindrome } from '../CheckPalindrome'
1+
import { checkPalindrome } from '../CheckPalindrome.js'
22

33
describe('checkPalindrome', () => {
4-
it('expects to return "Palindrome" if the given string is a palindrome', () => {
5-
const SUT = checkPalindrome('madam')
6-
expect(SUT).toBe('Palindrome')
4+
test('racecar is a palindrome', () => {
5+
expect(checkPalindrome('racecar')).toBe(true)
76
})
8-
it('expects to return "Empty string" if the given string is empty', () => {
9-
const SUT = checkPalindrome('')
10-
expect(SUT).toBe('Empty string')
7+
8+
test('hello is NOT a palindrome', () => {
9+
expect(checkPalindrome('hello')).toBe(false)
10+
})
11+
12+
test('A man a plan a canal Panama is a palindrome', () => {
13+
expect(checkPalindrome('A man a plan a canal Panama')).toBe(true)
14+
})
15+
16+
test('empty string is a palindrome', () => {
17+
expect(checkPalindrome('')).toBe(true)
18+
})
19+
20+
test('single character is a palindrome', () => {
21+
expect(checkPalindrome('a')).toBe(true)
1122
})
12-
it('expects to return "Not a string" if the given string is not a string', () => {
13-
const SUT = checkPalindrome(123)
14-
expect(SUT).toBe('Not a string')
23+
24+
test('madam is a palindrome', () => {
25+
expect(checkPalindrome('madam')).toBe(true)
26+
})
27+
28+
test('12321 is a palindrome', () => {
29+
expect(checkPalindrome('12321')).toBe(true)
30+
})
31+
32+
test('12345 is NOT a palindrome', () => {
33+
expect(checkPalindrome('12345')).toBe(false)
34+
})
35+
36+
test('throws TypeError for non-string input', () => {
37+
expect(() => checkPalindrome(123)).toThrow(TypeError)
1538
})
1639
})

0 commit comments

Comments
 (0)