Open
Description
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; }
Metadata
Metadata
Assignees
Labels
No labels