Wordle 1: implementing the Wordle puzzle logic

First, set the browser to Full Screen view, to give this worksheet and your code as much space as possible.

Then choose either one of the two options below:

Option 1: Start this worksheet from scratch

Option 2: Continue, or view, previous work

This worksheet is copyright © Richard Pawson 2025, and protected by the Creative Commons license: Attribution-NonCommercial-NoDerivatives 4.0 International. You may freely make and distribute copies of this worksheet as is, but if you modify this worksheet you may not distribute your modified version (outside your own teaching institution) without the author's permission. Please email the author to report errors or suggest improvements.

Step 1: introduction

We are going to create a working implementation of the Wordle puzzle. If you have never previously solved a Wordle puzzle, you should visit the official Wordle site and solve today's puzzle, so you know how it works.

Notes

Total hints used: /

Step 2: mark green matches correctly

If you were telling a person how to mark an attempt word against a target word manually, you would probably tell them to look for any green matches first. (It's far more complicated to try to do it any other way.) In the test you can see that the first few failing test cases (each assert instruction is one test case) are all testing only for green matches. So, your first task is to modify the markAttempt function such that the three 'Only greens' tests pass. Here are some initial ideas that will help:
Hint 2-1: more detail
Hint 2-2: the new instructions in outline form
Hint 2-3: the instruction to modify the mark
Hint 2-4: the complete expression for the modified mark
Hint 2-5: the complete code for the function

Notes

Total hints used: /

Step 3: start on the yellow matches

Hint 3-1: the condition
Hint 3-2: outline for the second loop
Hint 3-3: the complete function (so far)

What is that unwanted consequence and why do you think this has happened? (Look at the individual cases in the tests).

We will fix this issue in the next step.

Notes

Total hints used: /

Step 4: mark the yellows without over-writing greens'

The problem is: letters that were marked green in the first loop are being 'overwritten' by a yellow. In the second loop we need to exclude any characters that have already been marked green. Attempt it now by modifying the if condition within the second loop. You should get all the 'Green only' cases passing again, and all except one of the 'Yellow only' cases.

Hint 4-1: comparing for 'inequality'
Hint 4-2: the new part of the condition
Hint 4-3: the full condition
Hint 4-4: the complete function

Why is one of the 'Only yellows' cases still failing? (Look at what is different about that specific case)

We will address that failing test shortly, but first we are going to do a little bit of 'refactoring' - a hugely important practice when writing good code.

Notes

Total hints used: /

Step 5: factor-out common code into a separate function

Before moving on to dealing with the one remaining failure of the 'Only yellows' test cases, we going to deal with what professional developers call a 'code smell'! The two expressions that are used to update the mark are very similar: set markvariableName? to mark[..i] + "2" + mark[i + 1..]value or expression?0 set markvariableName? to mark[..i] + "1" + mark[i + 1..]value or expression?0 Applying the 'DRY' principle (Don't Repeat Yourself) we should extract this common code into a separate function. The two expressions aren't identical so we need to 'parameterise' the expression - passing just the part that differs as a parameter into the function.

Hint 5-1: the setChar function with stub implementation
Hint 5-2: the complete code for setChar
Hint 5-3: one of the changes to be made in markAttempt
Hint 5-4: the complete markAttempt (so far) with the changes made

We have now completed this refactoring and can return to further refining the markAttempt function to get more of the test cases passing.

Notes

Total hints used: /

Step 6: fix the remaining 'Only yellows' test

The 'Only yellows' test is still failing because the three Ps in the attempt word are all being matched against the single P in the target word, which is incorrect - just the first one should be marked as a yellow match. To fix this we need to ensure that the loop managing the yellow matches does not use a letter from the target word more than once.

Hint 6-1: the variable definition and change to condition
Hint 6-2: updating 'unused' - key ideas
Hint 6-3: a useful expression
Hint 6-4: the complete new instruction
Hint 6-5: the complete markAttempt function

If you have implemented this step correctly, you should see that the last failing 'Only yellows' test case is now passing.

How many of the 'Mixed' test cases are now still failing? Can you figure out from the tests what they have in common?

You might wonder why we didn't also change target to unused in the first condition. It wouldn't matter if you made that change - try it.

Can you explain why that change - within the greens loop - doesn't make any difference, when it did make a difference within the yellows loop?

Notes

Total hints used: /

Step 7: Fix the remaining 'Mixed' test cases

Several of the 'Mixed' test cases are now passing as a result of fixing up the 'Greens only' and 'Yellows only' cases. If you look carefully you will see that for the remaining failures the function is wrongly re-using a character that has been marked green to match a different character as yellow. All we should need to do to fix this, is to ensure that whenever a green match is identified, that same character is also blanked out from unused.

Within the first loop, underneath the instruction that sets the mark, add a new instruction to set the same character number in unused to a single empty space. (The instruction to do this is slightly simpler than the one you have just added in the second loop.)

Hint 7-1: the new instruction to be added in within the first loop
Hint 7-2: complete markAttempt function after the changes so far

When you have completed this change, all the test cases for markAttempt should now be passing.

So if you now run the program again, it should work correctly. Each time you run it the computer picks a different target work - selected at random from the 2,315 valid target words.

Notes

Total hints used: /

Congratulations! Worksheet completed

You have just implemented the core logic of the Wordle puzzle - how to mark attempt words against a known target word - in just 14 instructions! And you have learned or practised several important programming patterns and techniques, including:

If you would like to share the working application with others, click file > save as standalone to save it as a self-contained single web page that shows only an enlarged version of the display tab, and runs the program automatically when it is opened. This standalone Html file may be emailed to others. You might like to print a few more instructions within main and/or add any of the refinements below.

You now have the option to

of the finished program. The initial constants are huge, so use the outline button to make it easier to explore. This will allow you to see how it is implementing the functionality that you haven't written - for example the user interface. You can save this source code to your own local file and then modify it as you wish.

There is a follow-on worksheet to this over-writing - 'Wordle 2' - in which you will re-use the markAttempt function you have just written to write a 'reverse game' (also described as an 'automated solver') for Wordle. Incredible as this might seem, you will be able to implement the solving algorithm by writing just 12 more instructions.

Meantime, you might like to consider undertaking one or more of the following suggested improvements/extensions to the current implementation.

Optional extensions