How to Create a Simple Word Unscrambler With JavaScript

Simon Ugorji
JavaScript in Plain English
4 min readAug 24, 2022

--

Photo by Brett Jordan on Unsplash

Assuming you’ve been given a long set of letters that appears to be a scrambled word and you’re asked to unscramble it, how would you unscramble the word?

Today, I will show you how you can build a function that will help you to unscramble any word as long as the word exists in our dictionary.

Getting Started

We need to set up our project by installing a very nice dictionary that will be used to filter the words.

On your local server,

  • Install the npm package “an-array-of-english-words”
$ npm install an-array-of-english-words
  • Or download it from Github

Building Our Function

Now that we have our dictionary installed, we need to build a function that will help us unscramble a word, making use of the words in our dictionary.

This function will have just one parameter which is the word to unscramble.

Importing Our Dictionary

Now we need to import our dictionary into the function, to help us filter the words based on our logic.

If the require statement isn’t allowed in your development environment, you have to import the dictionary and work with it as a module.

Filtering Through The Dictionary

This is the most important section of our function. In this section, I will write very simple logic to filter through the dictionary and return the word(s) that we are looking for.

I will use Array.filter() on the dictionary, and the first step is to check if the length of the word we are trying to unscramble is equal to the length of the current item in the dictionary. This will save us a huge amount of time and memory as well as make our logic neater.

Handling A Reoccurrence

Because I don’t know the word that you are trying to unscramble;

  • I will retrieve the current item in the dictionary
  • Convert it to an array using the split() method
  • Loop through the array
  • Then store the number of reoccurrences of each letter in a variable reOccurence1

I will do the same to the scrambled word provided but in a different variable reOccurence2.

Breaking It Down

To make you understand the code above, take, for example, there’s a word in the dictionary with the name “simon”, and you’re trying to unscramble the word “mnios”.

If our function should execute up to the reoccurrence section, we will have exactly these two objects containing each of the letters and the number of times they are repeated.

Just by looking at the objects, you will notice that they are equal and this means that we have found a word that matches the scrambled letters.

Unscrambling The Word

Having understood how reoccurrences are stored and handled, we now have to loop through the first object and check if the number of reoccurrences of the letters is the same as the other object.

If they are the same, then without a doubt, the current item in the dictionary is the word that we are looking for.

I will create a variable that will serve as our counter. This counter will increase on every match found while looping through the object.

Now if the counter is equal to the number of keys in the array, we will return the current item in the dictionary because that is the word that we are looking for.

Here’s the full code

Testing Our Function

Assuming I have these letters “izrbera”, and I want to unscramble it. This is how I will call the function.

console.log( unscramble("izrbera") );

This is the result

  • bizarre
  • brazier
word unscrambler in javascript

This function returns an array containing the words that match the scrambled letters, or an empty array if no words were found.

You’ve reached the end of my article.

Check out a live demo of this function here

Check out the NPM package here

Thank you for reading.

More content at PlainEnglish.io. Sign up for our free weekly newsletter. Follow us on Twitter, LinkedIn, YouTube, and Discord.

--

--