assets/js/X-letter.js in word-games-theme-0.8.0 vs assets/js/X-letter.js in word-games-theme-0.8.1

- old
+ new

@@ -1,10 +1,12 @@ --- --- const getScript=document.currentScript const letterLen = getScript.dataset.letter +const ablank = getScript.dataset.ablank + let errorMsg = document.querySelector('.errorMsg') let script = document.currentScript let wordCount = document.querySelector('.wordCount') let main = document.querySelector('.main') @@ -22,10 +24,20 @@ tab_link_wrapper.style.display = "none" let txtBox = document.querySelector('.txtBox') txtBox.focus() txtBox.value = serachValue + + +if(ablank){ +if(!serachValue.includes("?")){ + if(serachValue.length < letterLen){ + serachValue = serachValue + '?' + } + } +} + txtBox.addEventListener('input', (e) => { let rangeOfBlankTile = script.dataset.range e.target.value = e.target.value.replace(/[^a-zA-Z? ]/g, '') if (rangeOfBlankTile === '') { rangeOfBlankTile = 5 @@ -78,10 +90,11 @@ } // calling function getData(serachValue.toLowerCase()) function x_with_letters(data) { + console.log(data); if (typeof data === 'string') { errorMsg.innerHTML = 'No words found' wordCount.innerHTML = `<strong>Found 0 words with letters ${serachValue.split( '' )}</strong>` @@ -91,12 +104,12 @@ if (letterLen) { filterData = data.filter((item) => item.length == letterLen) } - + if (prefixValue) { filterData = filterData.filter((item2) => item2.startsWith(prefixValue.toLowerCase()) ) startsWith.classList.add('tick') @@ -314,12 +327,46 @@ newArray.sort(function (a, b) { return b.points - a.points }) }) const result = newArray.map((item) => { - return `<a class="anchor__style" title="Lookup python in Dictionary" target="_blank" href="/word-meaning?search=${item.words}"> - <li>${item.words} + var text1 = serachValue.replace('?', '') + var text2 = item.words + var text3 = item.words + function findIndex(str, char) { + const strLength = str.length + const indexes = [] + let newStr = str + while (newStr && newStr.indexOf(char) > -1) { + indexes.push(newStr.indexOf(char) + strLength - newStr.length) + newStr = newStr.substring(newStr.indexOf(char) + 1) + newStr = newStr.substring(newStr.indexOf(char) + 1) + } + return indexes + } + let chars = text1.split('') + let indexs = [] + chars.map((i) => { + let findIndexes = findIndex(text3, i) + if (findIndexes.length > 0) { + text3 = text3.split('') + text3[findIndexes] = '$' + text3 = text3.join('') + indexs = [...indexs, ...findIndexes] + } + }) + let itemHtml = '' + text2.split('').map((itemValue, index) => { + let check = indexs.find((i) => i === index) + if (check !== undefined) { + itemHtml += `${itemValue}` + } else { + itemHtml += `<span class='highlight'>${itemValue}</span>` + } + }) + return `<a class="anchor__style" title="Lookup ${item} in Dictionary" target="_blank" href="/word-meaning?search=${item.words}"> + <li>${itemHtml} <span class="points" value="${item.points}" style="position:relative; top:4px; font-size:12px"> ${item.points}</span> </li></a>` }) main.innerHTML += ` @@ -334,11 +381,10 @@ </div> </div> ` } } - // sort by aplhabets function sortby(sortBool, data, i) { if (sortBool) { main.innerHTML = '' data.reverse() @@ -347,12 +393,48 @@ let sum = 0 item = item.toLowerCase() for (let i = 0; i < item.length; i++) { sum += ScrabbleLetterScore[item[i]] || 0 // for unknown characters } - return `<a class="anchor__style" title="Lookup python in Dictionary" target="_blank" href="/word-meaning?search=${item}"> - <li>${item} + + var text1 = serachValue.replace('?', '') + var text2 = item + var text3 = item + function findIndex(str, char) { + const strLength = str.length + const indexes = [] + let newStr = str + while (newStr && newStr.indexOf(char) > -1) { + indexes.push(newStr.indexOf(char) + strLength - newStr.length) + newStr = newStr.substring(newStr.indexOf(char) + 1) + newStr = newStr.substring(newStr.indexOf(char) + 1) + } + return indexes + } + let chars = text1.split('') + let indexs = [] + chars.map((i) => { + let findIndexes = findIndex(text3, i) + if (findIndexes.length > 0) { + text3 = text3.split('') + text3[findIndexes] = '$' + text3 = text3.join('') + indexs = [...indexs, ...findIndexes] + } + }) + let itemHtml = '' + text2.split('').map((itemValue, index) => { + let check = indexs.find((i) => i === index) + if (check !== undefined) { + itemHtml += `${itemValue}` + } else { + itemHtml += `<span class='highlight'>${itemValue}</span>` + } + }) + + return `<a class="anchor__style" title="Lookup ${item} in Dictionary" target="_blank" href="/word-meaning?search=${item}"> + <li>${itemHtml} <span class="points" value="${sum}" style="position:relative; top:4px; font-size:12px"> ${sum}</span> </li></a>` }) main.innerHTML += ` @@ -376,11 +458,47 @@ let sum = 0 item = item.toLowerCase() for (let i = 0; i < item.length; i++) { sum += ScrabbleLetterScore[item[i]] || 0 // for unknown characters } - return `<a class="anchor__style" title="Lookup python in Dictionary" target="_blank" href="/word-meaning?search=${item}"> - <li>${item} + + var text1 = serachValue.replace('?', '') + var text2 = item + var text3 = item + function findIndex(str, char) { + const strLength = str.length + const indexes = [] + let newStr = str + while (newStr && newStr.indexOf(char) > -1) { + indexes.push(newStr.indexOf(char) + strLength - newStr.length) + newStr = newStr.substring(newStr.indexOf(char) + 1) + newStr = newStr.substring(newStr.indexOf(char) + 1) + } + return indexes + } + let chars = text1.split('') + let indexs = [] + chars.map((i) => { + let findIndexes = findIndex(text3, i) + if (findIndexes.length > 0) { + text3 = text3.split('') + text3[findIndexes] = '$' + text3 = text3.join('') + indexs = [...indexs, ...findIndexes] + } + }) + let itemHtml = '' + text2.split('').map((itemValue, index) => { + let check = indexs.find((i) => i === index) + if (check !== undefined) { + itemHtml += `${itemValue}` + } else { + itemHtml += `<span class='highlight'>${itemValue}</span>` + } + }) + + return `<a class="anchor__style" title="Lookup ${item} in Dictionary" target="_blank" href="/word-meaning?search=${item}"> + <li>${itemHtml} <span class="points" value="${sum}" style="position:relative; top:4px; font-size:12px"> ${sum}</span> </li></a>` }) main.innerHTML += `