I am in the process of creating a word game that involves users typing letters on a board to form meaningful words. If the typed word matches any word in a JSON file, the user earns a point. I have successfully implemented the basic functionalities of the game. However, I have encountered an issue with my linear algorithm when trying to search through approximately 400k words in the JSON file. Can anyone suggest a more efficient algorithm for this task? Below is a glimpse of how the interface looks - random letters appearing on the board.
https://i.sstatic.net/B5oSR.png
Here is a snippet from my JSON file:
{"a": 1, "aa": 1, "aaa": 1, ...
This is how my JavaScript code currently appears:
document.getElementById('button').addEventListener('click', loadData);
const letters = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o',
'p','q','r','s','t','u','v','w','x','y','z'];
var searched_word = '';
var boxes = document.querySelectorAll('.square');
function clickBox() {
boxes.forEach(function(box) {
box.addEventListener('click', function(e) {
e.preventDefault();
searched_word += this.innerHTML;
document.querySelector('.input').value = searched_word;
});
});
return searched_word;
}
function randomLetters(boxes) {
for (var i = 0; i < boxes.length; i++) {
let randomLetterNumber = Math.floor(Math.random() * letters.length);
boxes[i].innerHTML = letters[randomLetterNumber].toLocaleUpperCase();
}
}
function loadData() {
const xhr = new XMLHttpRequest();
xhr.open('GET', 'words_dictionary_full.json', true);
xhr.onload = function() {
if (this.status === 200) {
const words = JSON.parse(this.responseText);
for (word in words) {
if (word === clickBox().toLowerCase()) {
console.log('true');
} else {
console.log('false');
}
}
}
};
xhr.send();
}
clickBox();
randomLetters(boxes);