Skip to content

Commit 6362cc9

Browse files
dev-madhurendramadhuredra
and
madhuredra
authored
added an algo for checking the string i palindrome or not (TheAlgorithms#1366)
Co-authored-by: madhuredra <madhuredra.tiwari@zemosolabs.com>
1 parent f5188dd commit 6362cc9

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

Maths/Palindrome.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,17 @@ const PalindromeIterative = (string) => {
4545
return true
4646
}
4747

48-
export { PalindromeIterative, PalindromeRecursive }
48+
/**
49+
*
50+
* Checks if a string is a palindrome.
51+
* @author dev-madhurendra
52+
* @param {string} str - The string to check.
53+
* @returns {boolean} True if the string is a palindrome, false otherwise.
54+
*
55+
* @example
56+
* const isPalindrome = checkPalindrome('racecar'); // Returns true
57+
* const isNotPalindrome = checkPalindrome('hello'); // Returns false
58+
*/
59+
const checkPalindrome = (str) => str.replace(/\s/g, '') === str.replace(/\s/g, '').split('').reverse().join('')
60+
61+
export { PalindromeIterative, PalindromeRecursive, checkPalindrome }

Maths/test/Palindrome.test.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { PalindromeRecursive, PalindromeIterative } from '../Palindrome'
1+
import { PalindromeRecursive, PalindromeIterative, checkPalindrome } from '../Palindrome'
22

33
describe('Palindrome', () => {
44
it('should return true for a palindrome for PalindromeRecursive', () => {
@@ -13,4 +13,13 @@ describe('Palindrome', () => {
1313
it('should return true for a non-palindrome for PalindromeIterative', () => {
1414
expect(PalindromeIterative('JavaScript')).toBeFalsy()
1515
})
16+
17+
it.each([
18+
['radar', true],
19+
['hello', false],
20+
['r', true],
21+
[' racecar ', true]
22+
])('should return value is palindrome or not', (value, expected) => {
23+
expect(checkPalindrome(value)).toBe(expected)
24+
})
1625
})

0 commit comments

Comments
 (0)