How To Create A Simple Word Unscrambler With JavaScript
Tags: javascript, tutorial, 100daysofcode, webdev
Published: 2022-08-24 06:35:00

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"

```bash
$ npm install an-array-of-english-words
```

- Or download it from Github

{% embed https://github.com/words/an-array-of-english-words %}

## 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.

```javascript
function unscramble(word){
  /* This function unscrambles a word
  * @word: This 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.

```javascript
function unscramble(word) {
    //import dictionary
    const dict = require('an-array-of-english-words');
}
```
> 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.

```javascript
function unscramble(word) {
    //import dictionary
    const dict = require('an-array-of-english-words');
    //return the result of our logic
    return(
        dict.filter( item => {
            //check if their lengths are equal
            if(item.length === word.length){
                //logic continuation here
            }
        })
    );
}
```      

### 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`.

```javascript
function unscramble(word) {
    //import dictionary
    const dict = require('an-array-of-english-words');
    //return the result of our logic
    return(
        dict.filter( item => {
            //handle reoccurrences
            const reOccurrence1 = {}
            const reOccurrence2 = {}
            //check if their lengths are equal
            if(item.length === word.length){
                //convert the current item to array and loop through
                item.split('').forEach(letter => {
                    //store the number of reoccurrences of each letter
                    reOccurrence1[letter] = reOccurrence1[letter] + 1 || 1;
                })
                //convert word to array and loop through
                word.split('').forEach(letter => {
                    //store the number of reoccurrences of each letter
                    reOccurrence2[letter] = reOccurrence2[letter] + 1 || 1;
                })
            }
        })
    );
}
```    

### 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.

![handling recoccurrence](https://cdn.hashnode.com/res/hashnode/image/upload/v1661306171448/bhVirlFjA.png)

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.

```javascript
function unscramble(word) {
    //import dictionary
    const dict = require('an-array-of-english-words');
    //return the result of our logic
    return(
        dict.filter( item => {
            //handle reoccurrences
            const reOccurrence1 = {}
            const reOccurrence2 = {}
            //check if their lengths are equal
            if(item.length === word.length){
                //convert the current item to array and loop through
                item.split('').forEach(letter => {
                    //store the number of reoccurrences of each letter
                    reOccurrence1[letter] = reOccurrence1[letter] + 1 || 1;
                })
                //convert word to array and loop through
                word.split('').forEach(letter => {
                    //store the number of reoccurrences of each letter
                    reOccurrence2[letter] = reOccurrence2[letter] + 1 || 1;
                })
                //counter to increase on every match found
                let match = 0;
                for(let key in reOccurence1){
                    if(reOccurence1[key] === reOccurence2[key]){
                        match++;
                    }
                }
            }
        })
    );
}
```       
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

```javascript
function unscramble(word) {
    //import dictionary
    const dict = require('an-array-of-english-words');
    //return the result of our logic
    return(
        dict.filter( item => {
            //handle reoccurrences
            const reOccurrence1 = {}
            const reOccurrence2 = {}
            //check if their lengths are equal
            if(item.length === word.length){
                //convert the current item to array and loop through
                item.split('').forEach(letter => {
                    //store the number of reoccurrences of each letter
                    reOccurrence1[letter] = reOccurrence1[letter] + 1 || 1;
                })
                //convert word to array and loop through
                word.split('').forEach(letter => {
                    //store the number of reoccurrences of each letter
                    reOccurrence2[letter] = reOccurrence2[letter] + 1 || 1;
                })
                //counter to increase on every match found
                let match = 0;
                for(let key in reOccurrence1){
                    if(reOccurrence1[key] === reOccurrence2[key]){
                        match++;
                    }
                }
                //return item
                return ( (Object.keys(reOccurrence1).length === match) ? item : false );
            }
            return;
        })
    );
}
```      
## TESTING OUR FUNCTION

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

```javascript
console.log( unscramble("izrbera") );
```       
This is the result

- bizarre
- brazier

![image.png](https://cdn.hashnode.com/res/hashnode/image/upload/v1661307914662/EAe-BzaSU.png)

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

{% embed https://octagon-simon.github.io/projects/unscrambler/ %}

Check out the NPM package here

{% embed https://www.npmjs.com/package/word-unscrambler %}

Image Credit: Brett Jordan on Unsplash

Thank you for reading.