File tree Expand file tree Collapse file tree 4 files changed +73
-0
lines changed Expand file tree Collapse file tree 4 files changed +73
-0
lines changed Original file line number Diff line number Diff line change @@ -11,3 +11,4 @@ Once inside, run the command below:
11
11
2 . Vowels counter
12
12
3 . Most recurring character
13
13
4 . Sentence Capitalization
14
+ 5 . Palindromes
Original file line number Diff line number Diff line change
1
+ // pick a solution and insert here to run the test.
2
+
3
+
4
+ function palindromeChecker ( text ) {
5
+ var textLen = text . length ;
6
+ for ( var i = 0 ; i < textLen / 2 ; i ++ ) {
7
+ if ( text [ i ] !== text [ textLen - 1 - i ] ) {
8
+ return false ;
9
+ }
10
+ }
11
+ return true ;
12
+ }
13
+
14
+ module . exports = palindromeChecker ;
Original file line number Diff line number Diff line change
1
+ // AN INTUITIVE APPROACH
2
+
3
+ function palindromeChecker ( text ) {
4
+
5
+ var reversedText = text . toLowerCase ( )
6
+ . split ( '' ) . reverse ( ) . join ( '' )
7
+
8
+ return text === reversedText
9
+ }
10
+
11
+
12
+ // LOOPING THROUGH AND COMPARING CHARACTERS
13
+
14
+
15
+ function palindromeChecker ( text ) {
16
+ let charArray = text . toLowerCase ( ) . split ( '' )
17
+
18
+ let result = charArray . every ( ( letter , index ) => {
19
+ return letter === charArray [ charArray . length - index - 1 ] ;
20
+ } )
21
+
22
+ return result
23
+ }
24
+
25
+ // LOOPING THROUGH AND COMPARING CHARACTERS(OPTIMIZED)
26
+
27
+
28
+ function palindromeChecker ( text ) {
29
+ var textLen = text . length ;
30
+ for ( var i = 0 ; i < textLen / 2 ; i ++ ) {
31
+ if ( text [ i ] !== text [ textLen - 1 - i ] ) {
32
+ return false ;
33
+ }
34
+ }
35
+ return true ;
36
+ }
Original file line number Diff line number Diff line change
1
+ const palindromeChecker = require ( './index' ) ;
2
+
3
+ test ( 'palindromeChecker is a function' , ( ) => {
4
+ expect ( typeof palindromeChecker ) . toEqual ( 'function' ) ;
5
+ } ) ;
6
+
7
+ test ( '"php" is a palindrome' , ( ) => {
8
+ expect ( palindromeChecker ( 'php' ) ) . toBeTruthy ( ) ;
9
+ } ) ;
10
+
11
+ test ( '" php " is not a palindrome' , ( ) => {
12
+ expect ( palindromeChecker ( ' php ' ) ) . toBeFalsy ( ) ;
13
+ } ) ;
14
+
15
+ test ( '"developer" is not a palindrome' , ( ) => {
16
+ expect ( palindromeChecker ( 'developer' ) ) . toBeFalsy ( ) ;
17
+ } ) ;
18
+
19
+ test ( '"2002" a palindrome' , ( ) => {
20
+ expect ( palindromeChecker ( '2002' ) ) . toBeTruthy ( ) ;
21
+ } ) ;
22
+
You can’t perform that action at this time.
0 commit comments