From 0e8ac06e2602ac257db301217f7505a1202e25bb Mon Sep 17 00:00:00 2001 From: remi Date: Sat, 11 Jun 2022 22:58:22 +0200 Subject: [PATCH] mess with how the code looks too much. sorry, yari >~< --- main.js | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/main.js b/main.js index 86f83bf..27e3b86 100644 --- a/main.js +++ b/main.js @@ -1,7 +1,6 @@ let json = null; let searchDictionary = {}; -let auxTranslatedDictionaryArray = []; let header = null; let main = null; @@ -232,7 +231,6 @@ function loadDictionary() { for (let o = 0; o < values[i].length; o++) { if (values[i][o].hasOwnProperty("translation")){ searchDictionary[keys[i]].push(stripWord(values[i][o]["translation"], true)) - auxTranslatedDictionaryArray.push(stripWord(values[i][o]["translation"], true)) }; if (values[i][o].hasOwnProperty("meaning")) searchDictionary[keys[i]].push(stripWord(values[i][o]["meaning"], true)); } @@ -251,7 +249,14 @@ function loadDictionary() { }); } -function search(word) { +function search(word, type) { + console.log(type); + if (type == "all") {word = ""}; + if (type == "random") { + let random = Math.floor(Math.random() * Object.keys(json).length); + return [Object.keys(json)[random]]; + } + console.log(word); let words = word.split(" "); for (let i = 0; i < words.length; i++) { words[i] = stripWord(words[i]); @@ -313,11 +318,15 @@ function doSearch(state = true) { } //Manage search methods - val = (val.indexOf("show:random") !== -1) ? auxTranslatedDictionaryArray[(Math.floor(Math.random() * auxTranslatedDictionaryArray.length))] : val; - val = (val.indexOf("show:all") !== -1) ? "" : val; + let type = "normal"; + if (val.includes("show:random")) { + type = "random"; + } else if (val.includes("show:all")) { + type = "all"; + } //Results - let results = search(val); + let results = search(val, type); if (results.length == 0) { main.innerHTML = "no results (todo: cool form)"; } else { @@ -436,17 +445,18 @@ function doSearch(state = true) { let header = document.createElement("div"); header.classList.add("results-header"); //When using search methods, accomodate search text. - switch(val) { + switch(type) { //show:all - case "": + case "all": val = "Showing all words"; break; - case "\"\"": - val = "Showing all words"; + case "random": + val = "Showing random word: " + results[0]; break; //OMG A WORD default: val = "search results for: " + val; + break; } header.innerHTML += "" + val + "
"; for (let i in types) {