Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit6362cc9

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 parentf5188dd commit6362cc9

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
returntrue
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+
constcheckPalindrome=(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)

[8]ページ先頭

©2009-2025 Movatter.jp