Word Play 5

Step 1
In this lesson we will be making a Scrabble helper to find the best possible word to place
Step 2
Before we begin coding, we should go over some terminology in Scrabble

The player has between one to seven tiles in their rack which are known as RACK TILES:


There are also tiles on the board for everyone to use which are known as BOARD TILES:


The rules of Scrabble let the player place tiles together in a single row or column. We will refer to sections of a row or column as SCOPES:

Step 3
We are creating a program that can take the rack tiles and certain scope of board tiles

This example will be used often in the project:

Step 4
Here are some other important examples to note

This one shows the minimum amount of characters for a proper Scrabble input. It must have at least one rack tile, one board letter tile, and one open board tile.


This one shows the maximum amount of characters for a proper Scrabble input. There can only be up to 7 rack tiles and the scope can be up to 15 tiles.


This last example shows a special case of using a blank rack tile (signified by the "?"). A blank rack tile can represent any letter when being placed and there are only 2 blank tiles available to all players.

Step 5
We will need to detect if input is a valid Scrabble input

Create a new function named isScrabbleValid inside the index.js file:

Step 6
Next we need to use the function to check if the input given is usable by Scrabble

Add this code to the checkInput function:

Step 7
Let's now check if the input is a valid size

Refer to Step 4 where we explain the minimum and maximum amount of characters for a valid Scrabble case.

Hint: The blank is checking if the input's length is less than the minimum amount of characters. The square brackets are counted in the length!

Check Your Work
Step 8
Save your code and test it out with some different examples

These examples should display these results:

17 18 19 20
Step 9
Let's now start validating the rack tiles

First we need to check the index of square brackets using the indexOf function, like so:


The rackStart needs to be the first character of input and rackEnd must hold at least one and at most seven characters.

Step 10
We then need to check if the rack contains only letters or question marks

We can reuse the isInvalid function which is nice.

Step 11
Since there is only two blank tiles in Scrabble, we need to check if there is more than two question marks in the rack

Add this function to index.js which will be able to count how many specific characters are in a string:


So to use this function to find how many question marks are in the rack, we can type count("?", rack);

Step 12
Let's now check if the input has more than two question marks using the new count function

Look at the previous step if you need help filling in the blank.

Add this inside the isScrabbleValid function:

Step 13
We now need to check if the board tiles are valid

Add this code to check if the board length is valid:

Step 14
We will also need to check if the board has only letters and asterisks

To easily accomplish this we will modify the isInvalid function to be able to select a special case character.

As of now, the special case was always a question mark, but now it will default to being a question mark unless we say otherwise.

Step 15
Now we can use this function to check if the board contains only letters and asterisks

Add this code to the isScrabbleValid function:

Step 16
The board needs to have at least one board tile and one open tile

We will use the count function to verify the number of open tiles.

Add this code:

Check Your Work
Step 17
Save your code

Check some examples of Scrabble input to see if it works.

Here's my example:

Step 18
Let's know prepare to get the possible scrabble combinations and display the results

Notice this code is similar to how we managed the crosswords and anagrams. Don't run the code yet because we still need to define two functions: getScrabbles and displayScrabbles

Step 19
Define two new functions that we called in the previous code

In case it isn't apparent:

getScrabbles will be getting all possible words that can be formed with the rack tiles given a scope of the board.

displayScrabbles will be displaying all of these possibilities from largest to smallest length of letters.

Step 20
Create variables to hold the possible scrabble solutions, as well as the rack and board tiles

Add this code inside getScrabbles:

Step 21
For the next step, we will need to find every scope within the starting scope

As an example, let's assume we start with this scope:


Each scope within this starting scope will be found in this order:


The start of the scope begins at the leftmost tile. It will check every ending tile in decreasing order. The start will then move to the right one tile and check each ending tile again.

Step 22
Now we need to find every possible scope within the original board tiles

The double for loops will find every possible combination and print it to the console

Check Your Work
Step 23
Save your code

Test out an example and see in the console if all the scopes for the board tiles appears

Step 24
We need to skip the scopes that have no board tiles

We can do this by checking if the board contains only asterisks

Step 25
If the number of asterisks in board is equal to the length of board, then skip this scope

To find the number of asterisks, you should use the count function. (We use it in the isScrabbleValid function).

Check Your Work
Step 26
Save your code

See if your example removes the scopes with only asterisks

Step 27
We also will want to skip scopes that have more empty tiles than the amount of rack tiles

In this example, the rack tiles are "abc" but the board has a scope of four empty tiles which is impossible to fill completely so we should skip the initial scope.

Step 28
If the number of asterisks in board is more than the length of racks, then skip this scope
Check Your Work
Step 29
Save your code

Make sure it works by using a similar example:

Step 30
Another scope error we need to look out for is having a board tile just outside the scope

This example shows two scopes that we shouldn't consider because there is a neighboring board tile just outside the scope.

Step 31
We need to check if the tiles just outside the scope are not open tiles

The first blank is checking the board tile one less than the start while the other blank is checking the board tile one more than the end.

Check Your Work
Step 32
Save your code

To test out the four if statements, try this input and see if the console shows this exact output:

Step 33
Now that we have every possible scope, we can start finding all possible Scrabble words

To find all possible solutions for a scope, we will need to follow this pseudocode:

FOR every word in words array
    IF word could be fit in the scope
        ADD to possible words array
FOR every word in possible words array
    IF word contains letters in rack
        ADD to scrabbles array
Step 34
Create three new functions to help us find all scrabble solutions

Add this code to index.js:


validScope will see if a certain word can fit in the scope.

validRack will see if a certain word contains letters in rack.

Step 35
Let's flesh out the findScrabbles function so it can indeed find all scrabbles

The previous two steps will help you know what code to write for the blanks.

Step 36
Next we need to check if a certain word can fit in the scope

Add this code to the validScope function:


The code first checks if the word is equal length to the scope. It then goes through each board letter in the scope and see if it matches the word's letter in the same index.

Step 37
Now we need to check if a possible word contains letter in rack

Add this code to the validRack function:


The code first converts the rack from a string into an array (we can easily delete items from arrays but not strings). Ignoring board letters, we then go through each new letter in the word and check if it is inside the rack array. If it is, then we remove it from the array and move on.

Step 38
Finally to test these three new function, let's print the results to the console

Change this code inside the getScrabbles function:

Check Your Work
Step 39
Save your code

Try out an example and see if the console displays all possible solutions

Step 40
Add the scrabble solutions to the scrabble array inside getScrabbles function
Step 41
Also add code to the displayScrabble function to show solutions from longest to shortest

Note: You can probably copy some of this code from a similar display function

Check Your Work
Step 42
Save your code

We should now see all Scrabble solutions displayed on the website!

Step 43
One problem we have is that blank rack tiles are being ignored

We will need to fix some logic inside the validRack function:

Step 44
Change some code inside the validRack function to allow blank tiles to be used

First find the number of blank tiles inside the rack (use the count function). Then use that number to allow a certain amount of tiles not found in rack.

Check Your Work
Step 45
Save your code

Test out an example to see if the blank tiles are working


Congrats we are done!

Word Play 5 Info


MVCode Clubs

Created By

Missing tiny aaronjau101


Word Play

Access Level


For Teachers and Schools

Teach coding to your students with MVCode Teach

MVCode offers an integrated product designed to teach coding to students.

Learn more about MVCode Teach