diff --git a/String/CheckPalindrome.js b/String/CheckPalindrome.js index 251e0b192f..d430bad6de 100644 --- a/String/CheckPalindrome.js +++ b/String/CheckPalindrome.js @@ -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 } diff --git a/String/test/CheckPalindrome.test.js b/String/test/CheckPalindrome.test.js index cfe88f7e53..6863524c57 100644 --- a/String/test/CheckPalindrome.test.js +++ b/String/test/CheckPalindrome.test.js @@ -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) }) })