Skip to content

added an algo for checking the string is palindrome or not #1366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion Maths/Palindrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,17 @@ const PalindromeIterative = (string) => {
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 }
11 changes: 10 additions & 1 deletion Maths/test/Palindrome.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PalindromeRecursive, PalindromeIterative } from '../Palindrome'
import { PalindromeRecursive, PalindromeIterative, checkPalindrome } from '../Palindrome'

describe('Palindrome', () => {
it('should return true for a palindrome for PalindromeRecursive', () => {
Expand All @@ -13,4 +13,13 @@ describe('Palindrome', () => {
it('should return true for a non-palindrome for PalindromeIterative', () => {
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)
})
})