added an algo for checking the string i palindrome or not (#1366)

Co-authored-by: madhuredra <madhuredra.tiwari@zemosolabs.com>
This commit is contained in:
Madhurendra Nath Tiwari
2023-09-23 21:30:10 +05:30
committed by GitHub
parent f5188ddf16
commit 6362cc967a
2 changed files with 24 additions and 2 deletions

View File

@ -45,4 +45,17 @@ const PalindromeIterative = (string) => {
return true 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 }

View File

@ -1,4 +1,4 @@
import { PalindromeRecursive, PalindromeIterative } from '../Palindrome' import { PalindromeRecursive, PalindromeIterative, checkPalindrome } from '../Palindrome'
describe('Palindrome', () => { describe('Palindrome', () => {
it('should return true for a palindrome for PalindromeRecursive', () => { 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', () => { it('should return true for a non-palindrome for PalindromeIterative', () => {
expect(PalindromeIterative('JavaScript')).toBeFalsy() 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)
})
}) })