Skip to content

18-6-anagrams Update(s): fixed anagram function to prevent invalid values. Created additional tests. #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
function anagram(str1, str2) {
// Clean the string of spaces, numbers, & special characters
// Return false when both words are the same
const pattern = /^[^A-Za-z]+/g;
const cleanStr1 = str1.replaceAll(pattern, "").toLowerCase();
const cleanStr2 = str2.replaceAll(pattern, "").toLowerCase();
if (cleanStr1 === cleanStr2) {
return false;
}

const aCharMap = buildCharMap(str1);
const bCharMap = buildCharMap(str2);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,9 @@ describe('Anagram', () => {
expect(anagram('hello', 'fellow')).toBeFalsy();
expect(anagram('world', 'twirl')).toBeFalsy();
expect(anagram('cost', 'lost')).toBeFalsy();
expect(anagram('hello', 'Hello')).toBeFalsy(); // same word
expect(anagram('', '')).toBeFalsy(); // empty strings
expect(anagram(' ', ' ')).toBeFalsy(); // whitespace
expect(anagram('3rd!', '!dr3')).toBeFalsy(); // invalid characters
});
});