Solving Valid Anagrams Checker Problem in JavaScript

Hello there! We are going to be talking about a fun game called Anagram. When you take a word and re-arrange its letter to make a new valid word that right there is called an Anagram. For example, the word “cat” can be re-arranged to form a new word “act”. Now imagine that you have a word in your mind (Pick any word) and you want to check if it’s a valid anagram or not, all you have to do is spell out the letters of that word in your mind to see if the letters can form a new word when spelled in a different order. If one of the words in your mind is “silent” after re-arranging the letters in the word we will get the word “listen”. Now that is an Anagram, so Easy.

To check if two words are anagrams we have to compare the letters in them. To do that we are going to be using JavaScript, which is my go-to language. After reading this you will get a clear concept of how to solve a valid anagram following this easy approach.

Create a function and covert the two words provided as an input to lowercase words inside of the function

function validAnagram(wordOne, wordTwo) {
 const wordOneLow = wordOne.toLowerCase();
 const wordTwoLow = wordTwo.toLowerCase();
}

Write a conditional statement to check the two words provided as an input has the same length. An anagram has the same number of letters if one word has more letters than the other then certainly that is not an anagram.

function validAnagram(wordOne, wordTwo) {
 const wordOneLow = wordOne.toLowerCase();
 const wordTwoLow = wordTwo.toLowerCase();

if (wordOneLow.length !== wordTwoLow.length) {
    return "Not An Anagram";
  }

}

We have to sort out each letter of both words into alphabetical order, so to do that we have to convert each letter into an array and afterward we sort them all alphabetically before joining each alphabetical letter into a string to form a word that is arranged in an alphabetical manner.

function validAnagram(wordOne, wordTwo) {
 const wordOneLow = wordOne.toLowerCase();
 const wordTwoLow = wordTwo.toLowerCase();

    if (wordOneLow.length !== wordTwoLow.length) {
        return "Not An Anagram";
      }

    const wordOneAlpha = wordOneLow.split("").sort().join("");
    const wordTwoAlpha  = wordTwoLow.split("").sort().join("");

}

Compare the two sorted words together to see if they are both arranged in the same alphabetical order. If the two words are the same after comparison then the two words are anagrams of each other.

function validAnagram(wordOne, wordTwo) {
 const wordOneLow = wordOne.toLowerCase();
 const wordTwoLow = wordTwo.toLowerCase();

    if (wordOneLow.length !== wordTwoLow.length) {
        return "Not An Anagram";
      }

    const wordOneAlpha = wordOneLow.split("").sort().join("");
    const wordTwoAlpha  = wordTwoLow.split("").sort().join("");

    if (wordOneAlpha === wordTwoAlpha) {
        return "Valid Anagram";
      } else {
        return "Not a Valid Anagram";
      }

}

In conclusion, the valid Anagram is a fun challenge we can easily solve now by breaking down the words into individual letters and comparing them both, we can easily know if two words are anagrams of each other.

1
Subscribe to my newsletter

Read articles from Zechariah Hounwanou directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Zechariah Hounwanou
Zechariah Hounwanou