const form = document.getElementById("form"); const input = document.getElementById("phrase"); const selectedDictionary = document.getElementById("dict"); const main = document.querySelector("#results"); const subResult = document.querySelector("#main-results"); const contains = document.querySelector("#contains"); const sort_dir = document.querySelector("#sort_dir"); const errorMessage = document.querySelector(".error-message"); const getScript = document.currentScript; let script = document.currentScript; const siteUrl = getScript.dataset.url; const min_letters = document.querySelector("#min_letters"); const max_words = document.querySelector("#max_words"); const limit = document.querySelector("#max_results"); let loadMore = document.querySelector("#loadMore"); let lazyLoader = document.getElementById("lazy-loader"); var resultArr = []; var found = new Set(); var scrabbleWordArr = []; input.focus(); var wcount = 0; var m_words = 0; var wlength = document.getElementById("word-length"); var max_word_length = wlength.dataset.word_length; function getCombinations(arr, n) { var i, j, k, elem, l = arr.length, childperm, ret = []; if (n == 1) { for (i = 0; i < arr.length; i++) { for (j = 0; j < arr[i].length; j++) { ret.push([arr[i][j]]); } } return ret; } else { for (i = 0; i < l; i++) { elem = arr.shift(); for (j = 0; j < elem.length; j++) { childperm = getCombinations(arr.slice(), n - 1); for (k = 0; k < childperm.length; k++) { ret.push([elem[j]].concat(childperm[k])); } } } return ret; } } let numberOfWords = 3; const getData = async (e) => { main.innerHTML = ""; e.preventDefault(); subResult.innerHTML = `
Finding words - Powered by anagramsolvers.org
`; const response = await fetch( `/.netlify/functions/getWords?name=${input.value.toLowerCase()}&selecteddictionary=${ selectedDictionary.value }` ); var data = await response.json(); max_word_length == "" ? (m_length = +max_words.value) : (m_length = max_word_length); // m_length is maximum word length if ( data != "No combination of these letters creates a word in the Engish language." ) { data = getCombo(data, m_length); } else { subResult.innerHTML = ""; errorMessage.style.display = "block"; document.querySelector(".heading-result").style.display = "none"; } }; form.addEventListener("submit", getData); function arraysEqual(a1, a2) { return JSON.stringify(a1.sort()) == JSON.stringify(a2.sort()); } async function getCombo(arr, m_words) { var userValue = input.value.trim().toLowerCase().split(""); var combinations = []; scrabbleWordArr = arr; if (max_word_length == "") { m_words = +max_words.value; } else { m_words = max_word_length; } arr.map((v) => { validWord(v.replace(" ", "").split(""), userValue, v); }); var combinations = arr.flatMap((v, i) => { return arr.slice(i + 1).map((w) => { var newWord = v + " " + w; validWord(newWord.replace(" ", "").split(""), userValue, newWord); return newWord; }); }); if (combinations.length > 0) { displayUi(resultArr, combinations, userValue, m_words, scrabbleWordArr); } } const equalsCheck = (a, b) => { a = a.sort(); b = b.sort(); return JSON.stringify(a) === JSON.stringify(b); }; function getComboWords(arr1, arr2, userValue) { var w_limit = limit.value; var count = 0; var combArr = []; console.log("inside combo"); arr2 = arr2.filter((ele) => { var repeat = getMatchedChars(ele, userValue); if (repeat) { return ele; } }); arr1 = arr1.filter((ele) => { var repeat = getRepeatedChars(ele, userValue); if (ele.replaceAll(" ", "").length <= userValue.length && repeat) { return ele; } }); for (let i = 0; i < arr1.length; i++) { var arr_word = arr1[i]; if ( arr_word != undefined && arr_word.replaceAll(" ", "").length < input.value.replaceAll(" ", "").length ) { for (let j = 0; j < arr2.length; j++) { var arr2_word = arr2[j]; var nWord = arr_word + " " + arr2_word; var nWord_w_space = nWord.replaceAll(" ", "").split(""); if ( nWord_w_space.length >= userValue.length && validWord(nWord_w_space, userValue, nWord) ) { count++; // // display([nWord]); // blockForFiveMilliSeconds(); printIndex = resultArr.length; // scrabbleWordArr.splice(j, 1); } combArr.push(nWord); } } if (count > 0 && count + resultArr.length >= w_limit) { break; } } combArr = combArr.filter((e) => e != undefined); return combArr; } function validWord(wordT, userValue, ele) { var sort_ele = ele.split(" ").sort().join(" "); if (arraysEqual(wordT, userValue) && !found.has(sort_ele)) { found.add(sort_ele); resultArr.push(ele); return true; } return false; } const getRepeatedChars = (str, userInput) => { var flag = false; const charsStr = {}; const charsUserInput = {}; for (const char of str.replace(" ", "")) { charsStr[char] = (charsStr[char] || 0) + 1; } for (const char of userInput.join("")) { charsUserInput[char] = (charsUserInput[char] || 0) + 1; } var str_repeat = Object.entries(charsStr) .filter((char) => char[1] > 1) .map((char) => char[0]); var input_repeat = Object.entries(charsUserInput) .filter((char) => char[1] > 1) .map((char) => char[0]); for (let i = 0; i < str_repeat.length; i++) { let c = str_repeat[i]; if (input_repeat.includes(c)) { var i_count = charsUserInput[c]; var str_count = charsStr[c]; if (str_count <= i_count) { flag = true; } } else { flag = false; break; } } return flag; }; const getMatchedChars = (str, userInput) => { var flag = false; const charsStr = {}; const charsUserInput = {}; for (const char of str.replace(" ", "")) { charsStr[char] = (charsStr[char] || 0) + 1; } for (const char of userInput.join("")) { charsUserInput[char] = (charsUserInput[char] || 0) + 1; } var str_repeat = Object.entries(charsStr) .filter((char) => char[1] >= 1) .map((char) => char[0]); var input_repeat = Object.entries(charsUserInput) .filter((char) => char[1] > 1) .map((char) => char[0]); for (let i = 0; i < str_repeat.length; i++) { let c = str_repeat[i]; if (userInput.includes(c)) { var i_count = charsUserInput[c]; var str_count = charsStr[c]; if (str_count <= i_count) { flag = true; } } else { flag = false; break; } } return flag; }; //n is max word async function displayUi(newdata, combinations, userValue, n, wordArr) { if (newdata.length == []) { if (found.size == 0) { subResult.innerHTML = ""; errorMessage.style.display = "block"; document.querySelector(".heading-result").style.display = "none"; } if (n > 2) { subResult.innerHTML = ""; errorMessage.style.display = "block"; document.querySelector(".heading-result").style.display = "none"; } } else { errorMessage.style.display = "none"; document.querySelector(".heading-result").style.display = "block"; if (sort_dir.value === "desc") { newdata = data.reverse(); } if (contains.value) { newdata = newdata.filter((item) => item.includes(contains.value.toLowerCase()) ); } requestAnimationFrame(() => { if (max_word_length != "") { newdata = newdata.filter((w) => { var arr1 = w.split(" "); if (arr1.length == max_word_length) { return w; } }); } newdata.map((item, index) => { var i = 0; let nWords = item.split(" "); var wordHtml = ""; nWords.map((word) => { wordHtml += `${word}`; }); var dataDiv = document.createElement("div"); dataDiv.className = "col-lg-3 col-md-4 col-sm-6 col-xs-6"; dataDiv.insertAdjacentHTML("beforeend", wordHtml); main.insertAdjacentElement("beforeend", dataDiv); subResult.innerHTML = ""; lazyLoader.style.display = "block"; }); resultArr = []; if (n > 2) { wordArr = wordArr.filter( (w) => w.length < Math.round(userValue.length / 2) ); var data = getComboWords(combinations, wordArr, userValue); n--; console.log("calling display ui from recursion"); if (data.length != 0) { displayUi(resultArr, data, userValue, n, wordArr); } else { if (found.size == 0 || main.innerHTML == "") { subResult.innerHTML = ""; errorMessage.style.display = "block"; document.querySelector(".heading-result").style.display = "none"; } found.clear(); wlength != null ? (m_words = wlength) : (m_words = +max_words.value); scrabbleWordArr = []; lazyLoader.style.display = "none"; } } else { found.clear(); wlength != null ? (m_words = wlength) : (m_words = +max_words.value); scrabbleWordArr = []; lazyLoader.style.display = "none"; } }); } } // function showLoader() { // lazyLoader.style.display = "block"; // loadMore.style.display = "none"; // }