How to Find an Anagram With JavaScript Using PEDAC
By: Itzel Rendon
Tags: Code, JavaScript, Coding, Problem Solving, Arrays

How to Find an Anagram With JavaScript Using PEDAC

In a coding interview, you really want to show your problem-solving skills, without looking like the guy on the cover. By using the PEDAC problem-solving method we will have a plan to solve any potential coding interview problem. For those who don’t know what PEDAC is, it’s an acronym for:

Problem Examples Data Algorithm Code

I will be going through each one to help clarify each step.

Problem:

Before any code is written, one must understand the problem. In this case, the problem is determining whether a list of given words contains an anagram of a word. Firstly, we must understand what an anagram is. According to our Google overlords, an anagram is a word or phrase whose letters can be rearranged to create another word or phrase. Now that we understand what an anagram is, we know that all of the letters from the original word must be found in the words of the given array. To better rephrase the problem, we can say that we will go through the array and check if any of the words have the same characters and number of characters as the original word. We have now explained the problem in our own words and can move on to the next step.

P: We need to determine which words from the list are anagrams of a given word and return those words.

Examples:

We must now create an example of how the program will work. We will provide inputs and outputs for this program. If we have the word `iceman` and we are provided with a list of words such as `anime, cinema, and google` we know that the output will be `cinema` because it is the only word that has the exact same letters as the word iceman just in a different order. Another example can be the word `listen` along with the list of words: `enlists, enlist, lists, inlets`. If those are the inputs, the outputs are `enlist, inlets`. Now that we’ve seen some examples of inputs and outputs, we can move to the next step which is Data.

E: (iceman, ["anime", "cinema", "google"]) => ["cinema"]

(listen, ["enlists", "enlist", "lists", "inlets", "words"]) => ["enlist", "inlets"]

(sushi, ["words", "youtube", "great", "hush"] => []

Data:

In this step, we will determine which data structures we will use. When we have a list of something we will most likely use an array. In this case, it is a good idea to use arrays because we can mutate the contents of the array making it easier to see if the words match one another. Data isn’t always going to be arrays, depending on the problem you might decide to use objects or strings or Booleans. By determining the data structure that you are using for the specific problem you can see which methods are available to be used. It will also be helpful if you had a specific method you wanted to use. This will help because it will give us a clue as to whether or not we will be converting the data type.

D: Array, because we are working with multiple items.

Algorithm:

In this step, we will determine a step by step solution and write it in pseudo-code. For this specific problem we know we need to find a way to compare each item to the original. That’s because each word that is an anagram of the original will be returned. A good question to ask is “how can we compare two words?” We can’t necessarily say if (inlets === listen) return inlets. It would always be false because they are not the same thing. We can use .sort() to change the words in an order that is comparable and then return the original word. By using sort we are changing the first word to its alphabetical order, and by doing the same to each word in the array we now have comparable data.

An example of what the algorithm looks like is:

A:

- Have a function that takes in two parameters a word and an array.

- Use some method or loop to iterate through the array

- For each item in the array and the target word, we will use sort to alphabetize it.

- In alphabetical order the two are indistinguishable.

- Whichever words match the target word will be stored in a variable (in case there is more than one found word)

- Return the variable that is holding the words that pass.

Algorithms may change as you code but the point is to have a plan that will make the coding part easier.

Code:

This is where we put all of the pieces together and code out our algorithm. If the algorithm is clear, the coding is a lot easier. There might be a couple of problems we hadn’t thought of but all of that can be fixed as long as we have a plan. Here is how we can code out the solution using the algorithm and what we know about sort.

function anagram(word, arr) {

//This variable holds the words from the array that match the word.

const found = [];

//We use forEach in order to iterate through the array and do the process of changing each //individual word without affecting the other words.

arr.forEach(item => {

//item is each item in the array

//by using split we are changing each item into a new array of each character

//We then sort to get the new array with the letters in alphabetical order

//We join the array to get a new word altogether

const newItem = item.split("").sort().join("");

//We do the same process for the word in order to have comparable data.

const newWord = word.split("").sort().join("");

//In this if statement we are comparing the newWord to the newItems in the array.

if (newWord === newItem) {

//when the words match we want to send the original item to the variable found.

//By sending the original item we have already compared it and it has passed true but we don't //want to permanently mutate the word.

found.push(item);

}

});

//We return the contents of found to see which words are anagram of the original word

return found;

}

anagram("iceman", ["anime", "cinema", "google", "words"]); //should return [ "cinema" ]

Code without comments:

function anagram(word, arr) {

const found = [];

arr.forEach(item => {

const newItem = item.split("").sort().join("");

const newWord = word.split("").sort().join("");

if (newWord === newItem) {

found.push(item);

}

});

return found;

}

anagram("iceman", ["anime", "cinema", "google", "words"]); //should return [ "cinema" ]

Conclusion…

I hope this helped. We all know that coding interviews may be anxiety-inducing but with PEDAC you have a plan of attack. Using PEDAC will provide you with a structure to follow and a way to really break down each problem into smaller parts. The problem won’t seem so big when broken down. I hope that you’ll find PEDAC as useful as I have!