Skip to content

Commit 6362cc9

Browse files
dev-madhurendramadhuredra
andauthored
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

Lines changed: 14 additions & 1 deletion
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

Lines changed: 10 additions & 1 deletion
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy