Wordle 1: implementing the Wordle puzzle logic
  1. Set the browser to Full Screen view, to give this worksheet and your code as much space as possible.
  2. to continue. (After that any entries made into the worksheet will be automatically saved, and you can re-load the partially-completed worksheet in future – at which point you will be asked to auto-save it again).
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.

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 just for just 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
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.

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 exlude any characters that have already been marked green. Attempt it now by modifying the if condition within in 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: the new part of the condition
Hint 4-2: the full condition
Hint 4-3: the complete function

Why is one of the 'Yellow only' cases still failing? (Look at what is different about the 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.

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. Total hints used: /

Step 6: keep track of which letters in the target have been 'used' as greens

The green tests are failing because letters that have already marked green are then being marked as yellow matches.
Hint 6-1: markAttempt showing changed functions in outline
Hint 6-2: complete markAttempt function after the changes so far

Has this fixed the failing 'Only yellows' test?

Has it fixed any other failing tests?

Total hints used: /

Step 7: get all remaining tests passing

There is still one 'Only yellows' test that 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. (We then have the 'Mixed' examples - containing both green and yellow matches to deal with!) To fix this we need to ensure that the second loop - managing the yellows matches - also does not use a letter from the target word more than once. Within this loop, underneath the instruction that sets the mark, add a new instruction that updates unused. Note, however, that this instruction will be different to the one you just wrote within the first loop. See if you can figure it out.
Hint 7-1: key ideas
Hint 7-2: a useful expression
Hint 7-3: the complete new instruction
Hint 7-4: the complete markAttempt function

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

If so, what else do you notice about the test results?

Total hints used: /

Congratulations! Worksheet completed

Perhaps you were surprised that in fixing all the 'Only greens' and 'Only yellows' tests, the 'Mixed' tests also pass. This sort of bonus doesn't always happen, but it shows the benefits of having plenty of test cases. As a result of this:

you will find that each time you run it the computer picks a different target work - selected at random from 2,309 valid target words.

Optional further work

If you have time, you might like to consider undertaking one or more of the following suggested improvements/extensions: Optional refinements