From 78c2b91f4b1ded25ddb8a3adb70ff97a8a6c5a2f Mon Sep 17 00:00:00 2001 From: Charlie Moore Date: Mon, 13 Sep 2021 20:29:23 -0400 Subject: [PATCH 1/4] add test cases for checkAnagram function to cover additional inputs and edge cases --- String/test/CheckAnagram.test.js | 46 +++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/String/test/CheckAnagram.test.js b/String/test/CheckAnagram.test.js index 691d5ba892..14ca401817 100644 --- a/String/test/CheckAnagram.test.js +++ b/String/test/CheckAnagram.test.js @@ -24,8 +24,52 @@ describe('checkAnagram', () => { const SUT = checkAnagram('abcs', 'abds') expect(SUT).toBe('Not anagrams') }) - it('expects to return "Anagram" if the arguments are anagram', () => { + it('expects to return "Anagram" if the arguments are anagrams', () => { const SUT = checkAnagram('abcd', 'bcad') expect(SUT).toBe('Anagrams') }) + it('expects to return "Anagrams" if the arguments of length 1 and are the same letter', () => { + const SUT = checkAnagram('a', 'a') + expect(SUT).toBe('Anagrams') + }) + it('expects to return "Anagrams" if the arguments of are both empty strings', () => { + const SUT = checkAnagram('', '') + expect(SUT).toBe('Anagrams') + }) + it('expects to return "Anagrams" if the arguments are anagrams with an odd length', () => { + const SUT = checkAnagram('abcde', 'edcab') + expect(SUT).toBe('Anagrams') + }) + it('expects to return "Anagrams" if the arguments are anagrams with an even length', () => { + const SUT = checkAnagram('abcdef', 'fedcab') + expect(SUT).toBe('Anagrams') + }) + it('expects to return "Not anagrams" if either argument is an empty string while the other is not', () => { + const SUT = checkAnagram('', 'edcab') + expect(SUT).toBe('Not anagrams') + const SUT2 = checkAnagram('edcab', '') + expect(SUT2).toBe('Not anagrams') + }) + it('expects to return "Not anagrams" if the arguments contain the same letters but have unequal case', () => { + const SUT = checkAnagram('ABDCE', 'abcde') + expect(SUT).toBe('Not anagrams') + const SUT2 = checkAnagram('AbCdE', 'aBCdE') + expect(SUT2).toBe('Not anagrams') + }) + it('expects to return "Anagrams" if the arguments are anagrams and contain number characters', () => { + const SUT = checkAnagram('a1b2', '12ba') + expect(SUT).toBe('Anagrams') + }) + it('expects to return "Anagrams" if the arguments are anagrams and contain space characters', () => { + const SUT = checkAnagram('a1 b2', '1 2ba') + expect(SUT).toBe('Anagrams') + }) + it('expects to return "Anagrams" if the arguments are anagrams and contain punctuation characters', () => { + const SUT = checkAnagram('a!1b@2', '1@2ba!') + expect(SUT).toBe('Anagrams') + }) + it('expects to return "Not anagrams" if the arguments contain the same letters but contain a different amount of space characters', () => { + const SUT = checkAnagram('ea cb', 'e cba') + expect(SUT).toBe('Not anagrams') + }) }) From d523f92c8b0f10332ee60af3af3961f572344dbc Mon Sep 17 00:00:00 2001 From: Charlie Moore Date: Tue, 21 Sep 2021 21:19:04 -0400 Subject: [PATCH 2/4] adjust spacing between tests to be more consistent with other files --- String/test/CheckAnagram.test.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/String/test/CheckAnagram.test.js b/String/test/CheckAnagram.test.js index 14ca401817..bd4b41752e 100644 --- a/String/test/CheckAnagram.test.js +++ b/String/test/CheckAnagram.test.js @@ -16,58 +16,71 @@ describe('checkAnagram', () => { expect(SUT).toBe('Not string(s)') } ) + it('expects to return "Not anagram" if the arguments have different lengths', () => { const SUT = checkAnagram('abs', 'abds') expect(SUT).toBe('Not anagrams') }) + it('expects to return "Not anagram" if the arguments are not anagrams', () => { const SUT = checkAnagram('abcs', 'abds') expect(SUT).toBe('Not anagrams') }) + it('expects to return "Anagram" if the arguments are anagrams', () => { const SUT = checkAnagram('abcd', 'bcad') expect(SUT).toBe('Anagrams') }) + it('expects to return "Anagrams" if the arguments of length 1 and are the same letter', () => { const SUT = checkAnagram('a', 'a') expect(SUT).toBe('Anagrams') }) + it('expects to return "Anagrams" if the arguments of are both empty strings', () => { const SUT = checkAnagram('', '') expect(SUT).toBe('Anagrams') }) + it('expects to return "Anagrams" if the arguments are anagrams with an odd length', () => { const SUT = checkAnagram('abcde', 'edcab') expect(SUT).toBe('Anagrams') }) + it('expects to return "Anagrams" if the arguments are anagrams with an even length', () => { const SUT = checkAnagram('abcdef', 'fedcab') expect(SUT).toBe('Anagrams') }) + it('expects to return "Not anagrams" if either argument is an empty string while the other is not', () => { const SUT = checkAnagram('', 'edcab') expect(SUT).toBe('Not anagrams') const SUT2 = checkAnagram('edcab', '') expect(SUT2).toBe('Not anagrams') }) + it('expects to return "Not anagrams" if the arguments contain the same letters but have unequal case', () => { const SUT = checkAnagram('ABDCE', 'abcde') expect(SUT).toBe('Not anagrams') const SUT2 = checkAnagram('AbCdE', 'aBCdE') expect(SUT2).toBe('Not anagrams') }) + it('expects to return "Anagrams" if the arguments are anagrams and contain number characters', () => { const SUT = checkAnagram('a1b2', '12ba') expect(SUT).toBe('Anagrams') }) + it('expects to return "Anagrams" if the arguments are anagrams and contain space characters', () => { const SUT = checkAnagram('a1 b2', '1 2ba') expect(SUT).toBe('Anagrams') }) + it('expects to return "Anagrams" if the arguments are anagrams and contain punctuation characters', () => { const SUT = checkAnagram('a!1b@2', '1@2ba!') expect(SUT).toBe('Anagrams') }) + it('expects to return "Not anagrams" if the arguments contain the same letters but contain a different amount of space characters', () => { const SUT = checkAnagram('ea cb', 'e cba') expect(SUT).toBe('Not anagrams') From 06fedc958a74ce3538e4195c72e7e413c39afcc6 Mon Sep 17 00:00:00 2001 From: Charlie Moore Date: Wed, 22 Sep 2021 13:51:01 -0400 Subject: [PATCH 3/4] update CheckAnagram to return boolean value instead of string --- String/CheckAnagram.js | 11 +++---- String/test/CheckAnagram.test.js | 56 ++++++++++++++++---------------- 2 files changed, 33 insertions(+), 34 deletions(-) diff --git a/String/CheckAnagram.js b/String/CheckAnagram.js index ea0eda8fad..a431eda96c 100644 --- a/String/CheckAnagram.js +++ b/String/CheckAnagram.js @@ -8,7 +8,7 @@ const checkAnagram = (str1, str2) => { // If both strings have not same lengths then they can not be anagram. if (str1.length !== str2.length) { - return 'Not anagrams' + return false } // Use hashmap to keep count of characters in str1 @@ -28,9 +28,8 @@ const checkAnagram = (str1, str2) => { for (let i = 0; i < str2.length; i++) { let previousCount = 0 // if str1CharCount has no key for str2[i] then not anagram. - if (!str1CharCount.has(str2[i])) { - return 'Not anagrams' - } + if (!str1CharCount.has(str2[i])) return false + previousCount = str1CharCount.get(str2[i]) str1CharCount.set(str2[i], previousCount - 1) } @@ -38,10 +37,10 @@ const checkAnagram = (str1, str2) => { // Now check if all entries in hashmap has zeros. for (const key in str1CharCount) { - if (str1CharCount[key] !== 0) { return 'Not anagrams' } + if (str1CharCount[key] !== 0) return false } - return 'Anagrams' + return true } export { checkAnagram } diff --git a/String/test/CheckAnagram.test.js b/String/test/CheckAnagram.test.js index bd4b41752e..ae1a3a593a 100644 --- a/String/test/CheckAnagram.test.js +++ b/String/test/CheckAnagram.test.js @@ -17,72 +17,72 @@ describe('checkAnagram', () => { } ) - it('expects to return "Not anagram" if the arguments have different lengths', () => { + it('expects to return false if the arguments have different lengths', () => { const SUT = checkAnagram('abs', 'abds') - expect(SUT).toBe('Not anagrams') + expect(SUT).toBe(false) }) - it('expects to return "Not anagram" if the arguments are not anagrams', () => { + it('expects to return false if the arguments are not anagrams', () => { const SUT = checkAnagram('abcs', 'abds') - expect(SUT).toBe('Not anagrams') + expect(SUT).toBe(false) }) - it('expects to return "Anagram" if the arguments are anagrams', () => { + it('expects to return true if the arguments are anagrams', () => { const SUT = checkAnagram('abcd', 'bcad') - expect(SUT).toBe('Anagrams') + expect(SUT).toBe(true) }) - it('expects to return "Anagrams" if the arguments of length 1 and are the same letter', () => { + it('expects to return true if the arguments of length 1 and are the same letter', () => { const SUT = checkAnagram('a', 'a') - expect(SUT).toBe('Anagrams') + expect(SUT).toBe(true) }) - it('expects to return "Anagrams" if the arguments of are both empty strings', () => { + it('expects to return true if the arguments of are both empty strings', () => { const SUT = checkAnagram('', '') - expect(SUT).toBe('Anagrams') + expect(SUT).toBe(true) }) - it('expects to return "Anagrams" if the arguments are anagrams with an odd length', () => { + it('expects to return true if the arguments are anagrams with an odd length', () => { const SUT = checkAnagram('abcde', 'edcab') - expect(SUT).toBe('Anagrams') + expect(SUT).toBe(true) }) - it('expects to return "Anagrams" if the arguments are anagrams with an even length', () => { + it('expects to return true if the arguments are anagrams with an even length', () => { const SUT = checkAnagram('abcdef', 'fedcab') - expect(SUT).toBe('Anagrams') + expect(SUT).toBe(true) }) - it('expects to return "Not anagrams" if either argument is an empty string while the other is not', () => { + it('expects to return false if either argument is an empty string while the other is not', () => { const SUT = checkAnagram('', 'edcab') - expect(SUT).toBe('Not anagrams') + expect(SUT).toBe(false) const SUT2 = checkAnagram('edcab', '') - expect(SUT2).toBe('Not anagrams') + expect(SUT2).toBe(false) }) - it('expects to return "Not anagrams" if the arguments contain the same letters but have unequal case', () => { + it('expects to return false if the arguments contain the same letters but have unequal case', () => { const SUT = checkAnagram('ABDCE', 'abcde') - expect(SUT).toBe('Not anagrams') + expect(SUT).toBe(false) const SUT2 = checkAnagram('AbCdE', 'aBCdE') - expect(SUT2).toBe('Not anagrams') + expect(SUT2).toBe(false) }) - it('expects to return "Anagrams" if the arguments are anagrams and contain number characters', () => { + it('expects to return true if the arguments are anagrams and contain number characters', () => { const SUT = checkAnagram('a1b2', '12ba') - expect(SUT).toBe('Anagrams') + expect(SUT).toBe(true) }) - it('expects to return "Anagrams" if the arguments are anagrams and contain space characters', () => { + it('expects to return true if the arguments are anagrams and contain space characters', () => { const SUT = checkAnagram('a1 b2', '1 2ba') - expect(SUT).toBe('Anagrams') + expect(SUT).toBe(true) }) - it('expects to return "Anagrams" if the arguments are anagrams and contain punctuation characters', () => { + it('expects to return true if the arguments are anagrams and contain punctuation characters', () => { const SUT = checkAnagram('a!1b@2', '1@2ba!') - expect(SUT).toBe('Anagrams') + expect(SUT).toBe(true) }) - it('expects to return "Not anagrams" if the arguments contain the same letters but contain a different amount of space characters', () => { + it('expects to return false if the arguments contain the same letters but contain a different amount of space characters', () => { const SUT = checkAnagram('ea cb', 'e cba') - expect(SUT).toBe('Not anagrams') + expect(SUT).toBe(false) }) }) From b0058b056b574201adb3038dedb77642069093f5 Mon Sep 17 00:00:00 2001 From: Charlie Moore Date: Wed, 22 Sep 2021 13:51:38 -0400 Subject: [PATCH 4/4] add a reference link and definition of Anagram to CheckAnagram documentation --- String/CheckAnagram.js | 1 + 1 file changed, 1 insertion(+) diff --git a/String/CheckAnagram.js b/String/CheckAnagram.js index a431eda96c..21ca1458d8 100644 --- a/String/CheckAnagram.js +++ b/String/CheckAnagram.js @@ -1,3 +1,4 @@ +// 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. // Anagram check is case sensitive; i.e. Aba and aba is not a anagram. // inputs are strings i.e. str1 and str2 const checkAnagram = (str1, str2) => {