mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-06 01:18:23 +08:00
merge: Add test Case for Palindrome Recursive (#855)
* Add test Case for Palindrome Recursive * Update Checks
This commit is contained in:
@ -1,21 +1,25 @@
|
|||||||
|
/**
|
||||||
|
* @function Palindrome
|
||||||
|
* @description Check whether the given string is Palindrome or not.
|
||||||
|
* @param {String} str - The input string
|
||||||
|
* @return {Boolean}.
|
||||||
|
* @see [Palindrome](https://en.wikipedia.org/wiki/Palindrome)
|
||||||
|
*/
|
||||||
|
|
||||||
// Check whether the given string is Palindrome or not
|
const palindrome = (str) => {
|
||||||
export const Palindrome = (str) => {
|
|
||||||
if (typeof str !== 'string') {
|
if (typeof str !== 'string') {
|
||||||
str = str.toString()
|
throw new TypeError('Invalid Input')
|
||||||
}
|
}
|
||||||
|
|
||||||
if (str === null || str === undefined) {
|
if (str.length <= 1) {
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
if (str.length === 1 || str.length === 0) {
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
if (str[0] !== str[str.length - 1]) {
|
if (str[0] !== str[str.length - 1]) {
|
||||||
return false
|
return false
|
||||||
} else {
|
} else {
|
||||||
return Palindrome(str.slice(1, str.length - 1))
|
return palindrome(str.slice(1, str.length - 1))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export { palindrome }
|
||||||
|
24
Recursive/test/palindrome.test.js
Normal file
24
Recursive/test/palindrome.test.js
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
import { palindrome } from '../Palindrome'
|
||||||
|
|
||||||
|
describe('Palindrome', () => {
|
||||||
|
it('expects to return true for palindrome string', () => {
|
||||||
|
const isPalindrome = palindrome('madam')
|
||||||
|
expect(isPalindrome).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('expects to return true for Empty String', () => {
|
||||||
|
const isPalindrome = palindrome('')
|
||||||
|
expect(isPalindrome).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('expects to return false for non-palindrome string', () => {
|
||||||
|
const isPalindrome = palindrome('foobar')
|
||||||
|
expect(isPalindrome).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('Throw Error for Invalid Input', () => {
|
||||||
|
expect(() => palindrome(123)).toThrow('Invalid Input')
|
||||||
|
expect(() => palindrome(null)).toThrow('Invalid Input')
|
||||||
|
expect(() => palindrome(undefined)).toThrow('Invalid Input')
|
||||||
|
})
|
||||||
|
})
|
Reference in New Issue
Block a user