File tree 4 files changed +75
-0
lines changed 4 files changed +75
-0
lines changed 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 isAnagram ( stringA , stringB ) {
5
+
6
+ const sanitizeString = function ( str ) {
7
+ return str . toLowerCase ( ) . replace ( / [ ^ a - z \d ] / g, '' ) . split ( '' ) . sort ( ) . join ( '' ) ;
8
+ }
9
+
10
+ return sanitizeString ( stringA ) == sanitizeString ( stringB )
11
+
12
+ }
13
+
14
+ module . exports = isAnagram ;
Original file line number Diff line number Diff line change
1
+ // DIRECT COMPARISON
2
+
3
+ function isAnagram ( stringA , stringB ) {
4
+
5
+ const sanitizeString = function ( str ) {
6
+ return str . toLowerCase ( ) . replace ( / [ ^ a - z \d ] / g, '' ) . split ( '' ) . sort ( ) . join ( '' ) ;
7
+ }
8
+
9
+ return sanitizeString ( stringA ) == sanitizeString ( stringB )
10
+
11
+ }
12
+
13
+ // CHARACTER MAP COMPARISON
14
+
15
+ function isAnagram ( stringA , stringB ) {
16
+
17
+ function createCharMap ( text ) {
18
+ let charMap = { }
19
+ for ( let char of text ) {
20
+ if ( charMap . hasOwnProperty ( char ) ) {
21
+ charMap [ char ] ++
22
+ } else {
23
+ charMap [ char ] = 1
24
+ }
25
+ }
26
+ return charMap
27
+ }
28
+
29
+ if ( stringA . length === stringB . length ) {
30
+
31
+ let stringAMap = createCharMap ( stringA )
32
+ let stringBMap = createCharMap ( stringB )
33
+
34
+ for ( let char in stringAMap ) {
35
+ if ( stringAMap [ char ] !== stringBMap [ char ] ) {
36
+ return false
37
+ }
38
+ }
39
+
40
+ return true
41
+ } else {
42
+ return false
43
+ }
44
+ }
Original file line number Diff line number Diff line change
1
+ const isAnagram = require ( './index' )
2
+
3
+ test ( 'isAnagram is a function' , ( ) => {
4
+ expect ( typeof isAnagram ) . toEqual ( 'function' )
5
+ } )
6
+
7
+ test ( '"dog" is an anagram of "god"' , ( ) => {
8
+ expect ( isAnagram ( 'dog' , 'god' ) ) . toBeTruthy ( )
9
+ } )
10
+
11
+ test ( '"Scotchy is Scotch!" is an anagram of "Scotch is Scotchy!"' , ( ) => {
12
+ expect ( isAnagram ( 'Scotchy is Scotch!' , 'Scotch is Scotchy!' ) ) . toBeTruthy ( )
13
+ } )
14
+
15
+ test ( '"I do not work weekends." is not an anagram of "I do not work weekdays!"' , ( ) => {
16
+ expect ( isAnagram ( 'I do not work weekends.' , 'I do not work weekdays!' ) ) . toBeFalsy ( )
17
+ } )
You can’t perform that action at this time.
0 commit comments