DEV Community

Discussion on: Anagrams Checker - Three JavaScript Solutions

Collapse
 
adamsstark profile image
Adams Stark

In order to remove spaces, you are calling string.replace once for every occurrence of " " (space) in each string. An easier way to remove whitespace in a string is to use a regular expression object with a global modifier, which will replace all matching characters in the string. Then, you can get rid of your while-loops, and your code becomes slightly easier to read. I hope it will help you to complete your Anagram checker project .

  ## function findAnagram (firstWord, secondWord) {
// "/ /g" is a regular expression object that finds all spaces in a string.
secondWord = secondWord.replace(/ /g, "");
firstWord = firstWord.replace(/ /g, "");
...

 ###
Enter fullscreen mode Exit fullscreen mode

You can also use the /\s/g regular expression object to replace all whitespace including tabs, newlines, etc.