From 019e205ceb9b34362969f436d51490bda87cfd40 Mon Sep 17 00:00:00 2001 From: Yatin Date: Sat, 27 Nov 2021 23:57:51 +0530 Subject: [PATCH 1/2] Add test Case for Palindrome Recursive --- Recursive/Palindrome.js | 22 +++++++++++++--------- Recursive/test/palindrome.test.js | 24 ++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 9 deletions(-) create mode 100644 Recursive/test/palindrome.test.js diff --git a/Recursive/Palindrome.js b/Recursive/Palindrome.js index 9357135174..f023bc1629 100644 --- a/Recursive/Palindrome.js +++ b/Recursive/Palindrome.js @@ -1,12 +1,14 @@ +/** + * @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 -export const Palindrome = (str) => { - if (typeof str !== 'string') { - str = str.toString() - } - - if (str === null || str === undefined) { - return false +const palindrome = (str) => { + if (typeof str !== 'string' || str === null || str === undefined) { + throw new TypeError('Invalid Input') } if (str.length === 1 || str.length === 0) { @@ -16,6 +18,8 @@ export const Palindrome = (str) => { if (str[0] !== str[str.length - 1]) { return false } else { - return Palindrome(str.slice(1, str.length - 1)) + return palindrome(str.slice(1, str.length - 1)) } } + +export { palindrome } diff --git a/Recursive/test/palindrome.test.js b/Recursive/test/palindrome.test.js new file mode 100644 index 0000000000..dee9b0af2e --- /dev/null +++ b/Recursive/test/palindrome.test.js @@ -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') + }) +}) From 789fe5f0513fa6dfe0bd9d27f2fcce15469d541b Mon Sep 17 00:00:00 2001 From: Yatin Date: Sun, 28 Nov 2021 12:16:14 +0530 Subject: [PATCH 2/2] Update Checks --- Recursive/Palindrome.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Recursive/Palindrome.js b/Recursive/Palindrome.js index f023bc1629..7d315aea31 100644 --- a/Recursive/Palindrome.js +++ b/Recursive/Palindrome.js @@ -7,11 +7,11 @@ */ const palindrome = (str) => { - if (typeof str !== 'string' || str === null || str === undefined) { + if (typeof str !== 'string') { throw new TypeError('Invalid Input') } - if (str.length === 1 || str.length === 0) { + if (str.length <= 1) { return true }