File tree 2 files changed +37
-9
lines changed
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
+ */
1
8
2
- // Check whether the given string is Palindrome or not
3
- export const Palindrome = ( str ) => {
9
+ const palindrome = ( str ) => {
4
10
if ( typeof str !== 'string' ) {
5
- str = str . toString ( )
11
+ throw new TypeError ( 'Invalid Input' )
6
12
}
7
13
8
- if ( str === null || str === undefined ) {
9
- return false
10
- }
11
-
12
- if ( str . length === 1 || str . length === 0 ) {
14
+ if ( str . length <= 1 ) {
13
15
return true
14
16
}
15
17
16
18
if ( str [ 0 ] !== str [ str . length - 1 ] ) {
17
19
return false
18
20
} else {
19
- return Palindrome ( str . slice ( 1 , str . length - 1 ) )
21
+ return palindrome ( str . slice ( 1 , str . length - 1 ) )
20
22
}
21
23
}
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