Skip to content

Commit

Permalink
Clean up search code
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisWhisker committed Apr 21, 2024
1 parent 954221f commit f638de2
Showing 1 changed file with 6 additions and 16 deletions.
22 changes: 6 additions & 16 deletions script.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
var buttonContainer;

// Wrap the code inside a DOMContentLoaded event listener to ensure it runs after the DOM is fully loaded
document.addEventListener("DOMContentLoaded", function() {
document.addEventListener("DOMContentLoaded", function () {
buttonContainer = document.getElementById("buttonContainer");

// Call the createButtons function initially
createButtons();
search("");
});

const wordCategories = {
Expand Down Expand Up @@ -36,11 +36,6 @@ function createButtons(strings) {
} else {
// If no strings are provided, create buttons for all categories and words
for (const category in wordCategories) {
// Create button for category
const categoryButton = document.createElement("button");
categoryButton.textContent = category;
buttonContainer.appendChild(categoryButton);

// Create buttons for words in category
for (const word of wordCategories[category]) {
const wordButton = document.createElement("button");
Expand All @@ -51,18 +46,18 @@ function createButtons(strings) {
}
}


// Find all words that contain the query
function search(query) {
const categoryResults = [];
const results = [];
query = query.toLowerCase();

for (const category in wordCategories) {
// Search the category name
if (category.includes(query)) {
console.log("Category: " + category);
categoryResults.push(category);

for (const word of wordCategories[category]) {
results.push(word);
}
}

// Search the words in the category
Expand All @@ -73,10 +68,5 @@ function search(query) {
}
}

// Add categories at the end
categoryResults.forEach(category => {
results.push(category);
});

createButtons(results);
}

0 comments on commit f638de2

Please sign in to comment.