snippet.host

minimal text and code snippet hosting

welcome, guest. why not login or register?

Wordle in Airtable script

created: 2 months ago views: 27 size: 4.64 KiB lines: 112 clone raw download
// Get tables and views.
// If you've duplicated this base,
// be sure to update these values to match those in your new base.
let players = base.getTable("tblqJDGko8n795jhq");
let dates = base.getTable("tbl9m6cWLI44ntAIO");
let todaysDate = dates.getView("viwI5MZYdKyOtrQda");
let guesses = base.getTable("tblf14y6enEKHn8NW");

// log player and check eligibility to play
let player = await input.recordAsync("Choose your name from the list", players);
if (player.getCellValue('Done today?') === 1) {
output.text("You've already played Wordle today -- come back tomorrow!")
} else {
// get today's Wordle info
let todaysRecord = await todaysDate.selectRecordsAsync({
fields: ["Date", "Word", "ID"]
});
let todaysRecordId = todaysRecord.recordIds;
let todaysWordle = todaysRecord.records[0].getCellValueAsString("Word");
let wordleNumber = todaysRecord.records[0].getCellValue("ID");

// prep variables for guesses
let playOn = 1;
let numberOfGuesses = 1;
let word;
let score = "\n";
let scoreFinal;

// begin the guessing loop
while (playOn = 1) {
let validityCheck = 1;
word = await input.textAsync("Type your guess for today's Wordle");
word = word.toLowerCase();

// check to see if the word is a valid word
let response = await fetch(`https://api.dictionaryapi.dev/api/v2/entries/en/${word}`, {
method: 'GET',
headers: {
'Accept': 'application/json'
}
}
);
let data = await response.json();
let wordCheck = data.title;

// check for word length = 5
if (word.length != 5) {
output.text('This word does not have 5 letters. Please try again.')
} else if (word.match(/[^a-zA-Z]/) != null) {
output.text('This word contains invalid characters. Please try again.')
} else if (wordCheck === "No Definitions Found") {
output.text('Word not found in dictionary, please use a valid word.')
} else {
validityCheck = 0;
let roundScore = "";

// check letter by letter for placed match, misplaced match, or no match
for (var i = 0; i < word.length; i++) {
// prep for double letter contingency
let letter = word[i];
let regex = new RegExp(letter,'g');
let newstr = todaysWordle.replace(regex,'');
let wordNewstr = word.substr(0, i+1).replace(regex,'');
// console.log('Letter:' + letter);
// console.log('Word w/o Letter:' + wordNewstr);
// console.log('Wordle w/o Letter:' + newstr);
// console.log('Word Stub # of Letters:' + (word.substr(0, i+1).length - wordNewstr.length));
// console.log('Wordle # of Letters:' + (todaysWordle.length - newstr.length));
if (word[i] === todaysWordle[i]) {
roundScore = roundScore + "🟩";
} else if (todaysWordle.includes(word[i])) {
if ((word.substr(0, i+1).length - wordNewstr.length) <= (todaysWordle.length - newstr.length)) { //conditional to prevent false hints due to double letters
roundScore = roundScore + "🟨"
} else {
roundScore = roundScore + "⬛️"
}
} else {
roundScore = roundScore + "⬛️"
}
}

// add the results from the round to the score variable
score = score + roundScore + "\n"

// victory condition
if (word === todaysWordle) {
playOn = 0;
scoreFinal = "Wordle " + wordleNumber + " - " + numberOfGuesses + "/6" + score;
output.text(scoreFinal);
break
} // defeat condition
else if (numberOfGuesses === 6) {
scoreFinal = "Defeat! " + wordleNumber + " - " + "X/6" + score;
numberOfGuesses = 0;
output.text(scoreFinal);
break
} // continue condition
else {
numberOfGuesses = numberOfGuesses + 1;
output.text(roundScore);
}
}
}

// create log of Wordle attempt
let newAttempt = await guesses.createRecordAsync({
'Player': [{id: player.id}],
'Date': [{id: todaysRecordId[0]}],
"Sharable score": scoreFinal,
"Score": numberOfGuesses
})
}