mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 16:26:47 +08:00
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:

committed by
GitHub

parent
f5188ddf16
commit
6362cc967a
@ -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 }
|
||||||
|
@ -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)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
Reference in New Issue
Block a user