4 ms·
Well... I'm kinda a jerk for this, but it's also a free lesson on why global variables are baaaaad. Also, I wrote this in seconds and I know it could be more e
by itslennysfault 4y ago
Well... I'm kinda a jerk for this, but it's also a free lesson on why global variables are baaaaad.
Also, I wrote this in seconds and I know it could be more efficient / cleaner. don't @ me.
Toss this in your browser's dev console to get the best word available for each puzzle instantly
function solvePuzzle(puzzle, words) {
const letters = {};
puzzle.forEach(l => {
if (!letters[l]) {
letters[l] = 0;
}
letters[l]++;
});
let best = '';
words.forEach(word => {
if (word.length > puzzle.length) {
return;
}
const has = { ...letters };
for (let i = 0; i < word.length; i++) {
if (!has[word[i]]) {
return;
}
has[word[i]]--;
}
if (word.length > best.length) {
best = word;
}
});
return best;
}
// lettersarray and wordlist are both globally available
solvePuzzle(lettersarray, wordlist);
- pstorm 4y ago> don't @ me I'm @ing you :) This code only checks for the longest word, not the highest scoring word. It neglects the x3 and x2 tile spaces.
- itslennysfault 4y agoFair point.