Public
Edited
May 16
Fork of Simple D3
2 stars
Insert cell
Insert cell
image = FileAttachment("wordle.png").image()
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
getWord = function (guess) {
return guess.toUpperCase().substring(0, 5);
}
Insert cell
getResult = function (guess) {
return guess.toUpperCase().substring(6, 11);
}
Insert cell
hasGreenOrYellowLetter = function (letter, guess, response) {
// Does our target word already have this letter
// if so then a second occurence may be marked Black
// but we don't want to eleimate all words containing this letter
for (let i = 0; i < response.length; i++) {
if (response[i] === "G" || response[i] === "Y") {
if (guess[i] === letter) {
return true;
}
}
}
return false;
}
Insert cell
blackFilter = function (testword, response) {
let blackLetters = "";
for (let i = 0; i < response.length; i++) {
if (response[i] === "B") {
// Wordle can mark the second occurance of a letter black
if (!hasGreenOrYellowLetter(testword[i], testword, response))
blackLetters += testword[i];
}
}
let rx = new RegExp("[" + blackLetters + "]");
// rerurn an anonymous closure that checks to make sure the word
// doesn't conatin any of the black letters
return (word) => !rx.test(word);
}
Insert cell
greenFilter = function (testword, response) {
let regextemplate = "";
for (let i = 0; i < response.length; i++) {
if (response[i] === "G") regextemplate += testword[i];
else regextemplate += ".";
}
let rx = new RegExp("^" + regextemplate + "$");
// return an anaonmyous closure that uses a regular exression to test each word
// for example RegExp("^A...Y$") matches all words starting with A and ending with Y.
return (word) => rx.test(word);
}
Insert cell
yellowFilter = function (testword, response) {
let notHere = []; // array of location, character pairs, and regex
for (let i = 0; i < response.length; i++) {
if (response[i] === "Y") {
notHere.push({
location: i,
character: testword[i],
regx: new RegExp("[" + testword[i] + "]")
});
}
}
// return an anonymous closure that checks all the yellow lettes
return (word) => {
let result = true;
notHere.forEach((spot) => {
if (spot.regx.test(word)) {
if (word[spot.location] == spot.character) {
result = false;
}
} else {
result = false;
}
});
return result;
};
}
Insert cell
## A note about dictionaries
Insert cell
The dictionary I am using is from [Language Corpus Data](https://norvig.com/ngrams/) and is the 8,939 word five letter subset of The Tournament Word List (178,690 words) -- used by North American Scrabble players. I believe the actual game uses a larger dictionary for legal words and a smaller one for actual target words. There is a small chance a target word might not be in the dictionary I am using. I did find the larger dictionary online but I could not verify rights ownership and I am lawyer adverse so I am not using it.
Insert cell
## A note about ethics

Insert cell
My wife believes that programs like this are cheating and should not only never be used, but never be created in the first place. In my defense I did this because I love coding and I wanted to prove to myself that I can still code at my age. I also did it as an excellent vehicle to show off the capabilities of Observable and ES6. That I could write less than 100 lines of code and have a functional solution to an interesting puzzle game, online and accessible to billions of users is incredibly amazing. I love Observable
In any case, if you use this helper and are going to boast/post about your prowess on social networks you should probably include a note that you did so. On a side note my wife is **much** better a [Wordle](https://www.powerlanguage.co.uk/wordle/) that I am, even when use this helper.
Insert cell
fiveLetterWordsString = FileAttachment("TWL05.txt").text()
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more