Skip to content

Commit 6f1edd1

Browse files
merge: Add test Case for Palindrome Recursive (#855)
* Add test Case for Palindrome Recursive * Update Checks
1 parent 027c0d6 commit 6f1edd1

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

Recursive/Palindrome.js

+13-9
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
1+
/**
2+
* @function Palindrome
3+
* @description Check whether the given string is Palindrome or not.
4+
* @param {String} str - The input string
5+
* @return {Boolean}.
6+
* @see [Palindrome](https://en.wikipedia.org/wiki/Palindrome)
7+
*/
18

2-
// Check whether the given string is Palindrome or not
3-
export const Palindrome = (str) => {
9+
const palindrome = (str) => {
410
if (typeof str !== 'string') {
5-
str = str.toString()
11+
throw new TypeError('Invalid Input')
612
}
713

8-
if (str === null || str === undefined) {
9-
return false
10-
}
11-
12-
if (str.length === 1 || str.length === 0) {
14+
if (str.length <= 1) {
1315
return true
1416
}
1517

1618
if (str[0] !== str[str.length - 1]) {
1719
return false
1820
} else {
19-
return Palindrome(str.slice(1, str.length - 1))
21+
return palindrome(str.slice(1, str.length - 1))
2022
}
2123
}
24+
25+
export { palindrome }

Recursive/test/palindrome.test.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { palindrome } from '../Palindrome'
2+
3+
describe('Palindrome', () => {
4+
it('expects to return true for palindrome string', () => {
5+
const isPalindrome = palindrome('madam')
6+
expect(isPalindrome).toBe(true)
7+
})
8+
9+
it('expects to return true for Empty String', () => {
10+
const isPalindrome = palindrome('')
11+
expect(isPalindrome).toBe(true)
12+
})
13+
14+
it('expects to return false for non-palindrome string', () => {
15+
const isPalindrome = palindrome('foobar')
16+
expect(isPalindrome).toBe(false)
17+
})
18+
19+
it('Throw Error for Invalid Input', () => {
20+
expect(() => palindrome(123)).toThrow('Invalid Input')
21+
expect(() => palindrome(null)).toThrow('Invalid Input')
22+
expect(() => palindrome(undefined)).toThrow('Invalid Input')
23+
})
24+
})

0 commit comments

Comments
 (0)