Skip to content

Commit 19970c3

Browse files
chore: merge "Tests check anagrams (#693)"
* add test cases for checkAnagram function to cover additional inputs and edge cases * adjust spacing between tests to be more consistent with other files * update CheckAnagram to return boolean value instead of string * add a reference link and definition of Anagram to CheckAnagram documentation
1 parent b399cf4 commit 19970c3

File tree

2 files changed

+69
-12
lines changed

2 files changed

+69
-12
lines changed

String/CheckAnagram.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// An [Anagram](https://en.wikipedia.org/wiki/Anagram) is a string that is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.
12
// Anagram check is case sensitive; i.e. Aba and aba is not a anagram.
23
// inputs are strings i.e. str1 and str2
34
const checkAnagram = (str1, str2) => {
@@ -8,7 +9,7 @@ const checkAnagram = (str1, str2) => {
89

910
// If both strings have not same lengths then they can not be anagram.
1011
if (str1.length !== str2.length) {
11-
return 'Not anagrams'
12+
return false
1213
}
1314

1415
// Use hashmap to keep count of characters in str1
@@ -28,20 +29,19 @@ const checkAnagram = (str1, str2) => {
2829
for (let i = 0; i < str2.length; i++) {
2930
let previousCount = 0
3031
// if str1CharCount has no key for str2[i] then not anagram.
31-
if (!str1CharCount.has(str2[i])) {
32-
return 'Not anagrams'
33-
}
32+
if (!str1CharCount.has(str2[i])) return false
33+
3434
previousCount = str1CharCount.get(str2[i])
3535
str1CharCount.set(str2[i], previousCount - 1)
3636
}
3737

3838
// Now check if all entries in hashmap has zeros.
3939

4040
for (const key in str1CharCount) {
41-
if (str1CharCount[key] !== 0) { return 'Not anagrams' }
41+
if (str1CharCount[key] !== 0) return false
4242
}
4343

44-
return 'Anagrams'
44+
return true
4545
}
4646

4747
export { checkAnagram }

String/test/CheckAnagram.test.js

Lines changed: 63 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,73 @@ describe('checkAnagram', () => {
1616
expect(SUT).toBe('Not string(s)')
1717
}
1818
)
19-
it('expects to return "Not anagram" if the arguments have different lengths', () => {
19+
20+
it('expects to return false if the arguments have different lengths', () => {
2021
const SUT = checkAnagram('abs', 'abds')
21-
expect(SUT).toBe('Not anagrams')
22+
expect(SUT).toBe(false)
2223
})
23-
it('expects to return "Not anagram" if the arguments are not anagrams', () => {
24+
25+
it('expects to return false if the arguments are not anagrams', () => {
2426
const SUT = checkAnagram('abcs', 'abds')
25-
expect(SUT).toBe('Not anagrams')
27+
expect(SUT).toBe(false)
2628
})
27-
it('expects to return "Anagram" if the arguments are anagram', () => {
29+
30+
it('expects to return true if the arguments are anagrams', () => {
2831
const SUT = checkAnagram('abcd', 'bcad')
29-
expect(SUT).toBe('Anagrams')
32+
expect(SUT).toBe(true)
33+
})
34+
35+
it('expects to return true if the arguments of length 1 and are the same letter', () => {
36+
const SUT = checkAnagram('a', 'a')
37+
expect(SUT).toBe(true)
38+
})
39+
40+
it('expects to return true if the arguments of are both empty strings', () => {
41+
const SUT = checkAnagram('', '')
42+
expect(SUT).toBe(true)
43+
})
44+
45+
it('expects to return true if the arguments are anagrams with an odd length', () => {
46+
const SUT = checkAnagram('abcde', 'edcab')
47+
expect(SUT).toBe(true)
48+
})
49+
50+
it('expects to return true if the arguments are anagrams with an even length', () => {
51+
const SUT = checkAnagram('abcdef', 'fedcab')
52+
expect(SUT).toBe(true)
53+
})
54+
55+
it('expects to return false if either argument is an empty string while the other is not', () => {
56+
const SUT = checkAnagram('', 'edcab')
57+
expect(SUT).toBe(false)
58+
const SUT2 = checkAnagram('edcab', '')
59+
expect(SUT2).toBe(false)
60+
})
61+
62+
it('expects to return false if the arguments contain the same letters but have unequal case', () => {
63+
const SUT = checkAnagram('ABDCE', 'abcde')
64+
expect(SUT).toBe(false)
65+
const SUT2 = checkAnagram('AbCdE', 'aBCdE')
66+
expect(SUT2).toBe(false)
67+
})
68+
69+
it('expects to return true if the arguments are anagrams and contain number characters', () => {
70+
const SUT = checkAnagram('a1b2', '12ba')
71+
expect(SUT).toBe(true)
72+
})
73+
74+
it('expects to return true if the arguments are anagrams and contain space characters', () => {
75+
const SUT = checkAnagram('a1 b2', '1 2ba')
76+
expect(SUT).toBe(true)
77+
})
78+
79+
it('expects to return true if the arguments are anagrams and contain punctuation characters', () => {
80+
const SUT = checkAnagram('a!1b@2', '1@2ba!')
81+
expect(SUT).toBe(true)
82+
})
83+
84+
it('expects to return false if the arguments contain the same letters but contain a different amount of space characters', () => {
85+
const SUT = checkAnagram('ea cb', 'e cba')
86+
expect(SUT).toBe(false)
3087
})
3188
})

0 commit comments

Comments
 (0)