Skip to content
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

18-6 Anagram - The anagram function fails to test for duplicate values or empty values #13

Open
fewdn opened this issue Jan 6, 2025 · 0 comments

Comments

@fewdn
Copy link

fewdn commented Jan 6, 2025

https://github.com/bradtraversy/javascript-sandbox/blob/main/18-unit-testing-algorithms/06-anagrams/algo-testing/anagram/anagram.js

The updated file can be found here:
PacktPublishing#3

The anagram function should return false if both words are the same or when one or both strings are empty.
Of course more could be done to remove special characters & numbers to clean the incoming values.

Currently invoking the following returns true but should be false:

anagram("pop", "pop");
anagram("", "");
anagram(" ", " ");

The quick fix could be a conditional and remove whitespace with replaceAll().

 const cleanStr1 = str1.replaceAll(" ", "").toLowerCase();
 const cleanStr2 = str2.replaceAll(" ", "").toLowerCase();
  if ( str1 === str2 ) { return false; }

Or, clean the string of special characters, numbers & whitespace, using a Regular Expression, then compare:

const pattern = /^[^A-Za-z]+/g;
const cleanStr1 = str1.replaceAll(pattern, "").toLowerCase();
const cleanStr2 = str2.replaceAll(pattern, "").toLowerCase();

if (cleanStr1 === cleanStr2) { return false; }
@fewdn fewdn changed the title 19 anagram - The anagram function fails to test for duplicate values or empty values 18 anagram - The anagram function fails to test for duplicate values or empty values Jan 6, 2025
@fewdn fewdn changed the title 18 anagram - The anagram function fails to test for duplicate values or empty values 18-6 Anagram - The anagram function fails to test for duplicate values or empty values Jan 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant