From 7a1e85f128554d4efedb8eb255c3b395b1b11786 Mon Sep 17 00:00:00 2001 From: madhuredra Date: Fri, 22 Sep 2023 15:49:18 +0530 Subject: [PATCH] added an algo for checking the string i palindrome or not --- Maths/Palindrome.js | 15 ++++++++++++++- Maths/test/Palindrome.test.js | 11 ++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/Maths/Palindrome.js b/Maths/Palindrome.js index 34804d34fb..f31e6aab15 100644 --- a/Maths/Palindrome.js +++ b/Maths/Palindrome.js @@ -45,4 +45,17 @@ const PalindromeIterative = (string) => { return true } -export { PalindromeIterative, PalindromeRecursive } +/** + * + * Checks if a string is a palindrome. + * @author dev-madhurendra + * @param {string} str - The string to check. + * @returns {boolean} True if the string is a palindrome, false otherwise. + * + * @example + * const isPalindrome = checkPalindrome('racecar'); // Returns true + * const isNotPalindrome = checkPalindrome('hello'); // Returns false + */ +const checkPalindrome = (str) => str.replace(/\s/g, '') === str.replace(/\s/g, '').split('').reverse().join('') + +export { PalindromeIterative, PalindromeRecursive, checkPalindrome } diff --git a/Maths/test/Palindrome.test.js b/Maths/test/Palindrome.test.js index 53cb883955..a8310e0a26 100644 --- a/Maths/test/Palindrome.test.js +++ b/Maths/test/Palindrome.test.js @@ -1,4 +1,4 @@ -import { PalindromeRecursive, PalindromeIterative } from '../Palindrome' +import { PalindromeRecursive, PalindromeIterative, checkPalindrome } from '../Palindrome' describe('Palindrome', () => { it('should return true for a palindrome for PalindromeRecursive', () => { @@ -13,4 +13,13 @@ describe('Palindrome', () => { it('should return true for a non-palindrome for PalindromeIterative', () => { expect(PalindromeIterative('JavaScript')).toBeFalsy() }) + + it.each([ + ['radar', true], + ['hello', false], + ['r', true], + [' racecar ', true] + ])('should return value is palindrome or not', (value, expected) => { + expect(checkPalindrome(value)).toBe(expected) + }) })