File tree 2 files changed +48
-0
lines changed
2 files changed +48
-0
lines changed Original file line number Diff line number Diff line change
1
+
2
+ // Solution 1
3
+ // Reverse the string and store the result in an variable than compare string to the reversed value
4
+ // function palindrome(str) {
5
+ // // const reverseStr = str.split("").reduce((rev, char) => char + rev, "");
6
+ // const reverseStr = str.split("").reverse().join("");
7
+ // return reverseStr === str;
8
+ // }
9
+
10
+ // Solution 2
11
+ function palindrome ( str ) {
12
+ return str . split ( '' ) . every ( ( char , i , arr ) => char === arr [ arr . length - i - 1 ] )
13
+ }
14
+ module . exports = palindrome ;
Original file line number Diff line number Diff line change
1
+ const palindrome = require ( './index' ) ;
2
+
3
+ test ( "palindrome function is defined" , ( ) => {
4
+ expect ( typeof palindrome ) . toEqual ( "function" ) ;
5
+ } )
6
+
7
+
8
+ test ( "'aba' is palindrome" , ( ) => {
9
+ expect ( palindrome ( "aba" ) ) . toBeTruthy ( ) ;
10
+ } )
11
+
12
+ test ( "' aba' is not palindrome" , ( ) => {
13
+ expect ( palindrome ( " aba" ) ) . toBeFalsy ( ) ;
14
+ } )
15
+
16
+ test ( "'aba ' is not palindrome" , ( ) => {
17
+ expect ( palindrome ( "aba " ) ) . toBeFalsy ( ) ;
18
+ } )
19
+
20
+ test ( '"greetings" is not a palindrome' , ( ) => {
21
+ expect ( palindrome ( 'greetings' ) ) . toBeFalsy ( ) ;
22
+ } ) ;
23
+
24
+ test ( '"1000000001" a palindrome' , ( ) => {
25
+ expect ( palindrome ( '1000000001' ) ) . toBeTruthy ( ) ;
26
+ } ) ;
27
+
28
+ test ( '"Fish hsif" is not a palindrome' , ( ) => {
29
+ expect ( palindrome ( 'Fish hsif' ) ) . toBeFalsy ( ) ;
30
+ } ) ;
31
+
32
+ test ( '"pennep" a palindrome' , ( ) => {
33
+ expect ( palindrome ( 'pennep' ) ) . toBeTruthy ( ) ;
34
+ } ) ;
You can’t perform that action at this time.
0 commit comments