Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions String/CheckPalindrome.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
// Palindrome check is case sensitive; i.e., Aba is not a palindrome
// input is a string
/**
* @function checkPalindrome
* @description A palindrome is a word, phrase, or sequence that reads the
* same backwards as forwards. This function checks if a given string
* is a palindrome, ignoring case and non-alphanumeric characters.
* @param {string} str - The input string to check
* @returns {boolean} - True if the string is a palindrome, false otherwise
* @see https://en.wikipedia.org/wiki/Palindrome
* @example checkPalindrome("racecar") => true
* @example checkPalindrome("hello") => false
* @example checkPalindrome("A man a plan a canal Panama") => true
*/
const checkPalindrome = (str) => {
// check that input is a string
if (typeof str !== 'string') {
return 'Not a string'
throw new TypeError('Input must be a string')
}
if (str.length === 0) {
return 'Empty string'
}
// Reverse only works with array, thus convert the string to array, reverse it and convert back to string
// return as palindrome if the reversed string is equal to the input string
const reversed = [...str].reverse().join('')
return str === reversed ? 'Palindrome' : 'Not a Palindrome'
const cleaned = str.toLowerCase().replace(/[^a-z0-9]/g, '')
return cleaned === cleaned.split('').reverse().join('')
}

export { checkPalindrome }
43 changes: 33 additions & 10 deletions String/test/CheckPalindrome.test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,39 @@
import { checkPalindrome } from '../CheckPalindrome'
import { checkPalindrome } from '../CheckPalindrome.js'

describe('checkPalindrome', () => {
it('expects to return "Palindrome" if the given string is a palindrome', () => {
const SUT = checkPalindrome('madam')
expect(SUT).toBe('Palindrome')
test('racecar is a palindrome', () => {
expect(checkPalindrome('racecar')).toBe(true)
})
it('expects to return "Empty string" if the given string is empty', () => {
const SUT = checkPalindrome('')
expect(SUT).toBe('Empty string')

test('hello is NOT a palindrome', () => {
expect(checkPalindrome('hello')).toBe(false)
})

test('A man a plan a canal Panama is a palindrome', () => {
expect(checkPalindrome('A man a plan a canal Panama')).toBe(true)
})

test('empty string is a palindrome', () => {
expect(checkPalindrome('')).toBe(true)
})

test('single character is a palindrome', () => {
expect(checkPalindrome('a')).toBe(true)
})
it('expects to return "Not a string" if the given string is not a string', () => {
const SUT = checkPalindrome(123)
expect(SUT).toBe('Not a string')

test('madam is a palindrome', () => {
expect(checkPalindrome('madam')).toBe(true)
})

test('12321 is a palindrome', () => {
expect(checkPalindrome('12321')).toBe(true)
})

test('12345 is NOT a palindrome', () => {
expect(checkPalindrome('12345')).toBe(false)
})

test('throws TypeError for non-string input', () => {
expect(() => checkPalindrome(123)).toThrow(TypeError)
})
})
Loading