File tree Expand file tree Collapse file tree 2 files changed +37
-9
lines changed
Expand file tree Collapse file tree 2 files changed +37
-9
lines changed Original file line number Diff line number Diff line change 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 }
Original file line number Diff line number Diff line change 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+ } )
You can’t perform that action at this time.
0 commit comments