DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 963,503 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
Cover image for How To Create A Simple Word Unscrambler With JavaScript
Simon Ugorji
Simon Ugorji

Posted on • Updated on

How To Create A Simple Word Unscrambler With JavaScript

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
Enter fullscreen mode Exit fullscreen mode
  • Or download it from Github

GitHub logo words / an-array-of-english-words

List of ~275,000 English words

an-array-of-english-words

Build Downloads Size

List of ~275,000 English words Derived from the Letterpress word list.

Install

npm:

npm install an-array-of-english-words
Enter fullscreen mode Exit fullscreen mode

Use

var words = require('an-array-of-english-words')
console.log(words.filter(d => /fun/.test(d)))
Enter fullscreen mode Exit fullscreen mode

Yields:

[
  'antifungal',
  'antifungals',
  'bifunctional',
  'cofunction',
  'cofunctions',
  // …and many more
]
Enter fullscreen mode Exit fullscreen mode

API

anArrayOfEnglishWords

Array.<string> β€” List of all English words, lowercased.

CLI

Install the CLI globally:

npm i -g an-array-of-english-words
Enter fullscreen mode Exit fullscreen mode

Now run words to print all the words to stdout:

words
Enter fullscreen mode Exit fullscreen mode

Use grep to filter by pattern:

words | grep cheese
Enter fullscreen mode Exit fullscreen mode

Use egrep to filter with regular expressions:

words | egrep '^fun'            # start with 'fun'
words | egrep 'ification$'      # end with 'ification'
words | egrep 'ou?r$'           # end in 'or' or 'our'
Enter fullscreen mode Exit fullscreen mode

Use wc to find out how many…

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.

function unscramble(word){
  /* This function unscrambles a word
  * @word: This is the word to unscramble 
  */
}
Enter fullscreen mode Exit fullscreen mode

IMPORTING OUR DICTIONARY

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

function unscramble(word) {
    //import dictionary
    const dict = require('an-array-of-english-words');
}
Enter fullscreen mode Exit fullscreen mode

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.

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
            }
        })
    );
}
Enter fullscreen mode Exit fullscreen mode

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.

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;
                })
            }
        })
    );
}
Enter fullscreen mode Exit fullscreen mode

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

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.

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++;
                    }
                }
            }
        })
    );
}
Enter fullscreen mode Exit fullscreen mode

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

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;
        })
    );
}
Enter fullscreen mode Exit fullscreen mode

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") );
Enter fullscreen mode Exit fullscreen mode

This is the result

  • bizarre
  • brazier

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

Check out the NPM package here

word-unscrambler - npm

This package helps to unscramble any word as long as the word exists in the dictionary. Latest version: 1.0.2, last published: 11 hours ago. Start using word-unscrambler in your project by running `npm i word-unscrambler`. There are no other projects in the npm registry using word-unscrambler.

favicon npmjs.com

Image Credit: Brett Jordan on Unsplash

Thank you for reading.

Top comments (0)

Visualizing Promises and Async/Await 🀯

async await

☝️ Check out this all-time classic DEV post