/***************
SCRABBLER_JS
***************/
// grab some html elements
let form = document.querySelector('#form')
let wordCount = document.querySelector('.wordCount')
let main = document.querySelector('.main')
let errorMsg = document.querySelector('.errorMsg')
let script = document.currentScript
// getqueryUrl from form
const params = new URLSearchParams(window.location.search)
let serachValue = params.get('search').toLowerCase()
let prefixValue = params.get('prefix')
let containsValue = params.get('contains')
let suffixValue = params.get('suffix')
let exculdeValue = params.get('exculde')
let includeValue = params.get('include')
let lengthValue = params.get('length')
let dictonary = params.get('dictionary')
// advanced filter element grabs
let tick
let startsWith = document.getElementById('startsWith')
let mustInclude = document.getElementById('mustInclude')
let endsWith = document.getElementById('endsWith')
let exculdeWith = document.getElementById('exculdeWith')
let inculdeWith = document.getElementById('inculdeWith')
let wordLength = document.getElementById('wordLength')
let ok = true
let tab_container = document.querySelector('.tab_container')
var sortValue
var sortBool = false
let txtBox = document.querySelector('.txtBox')
txtBox.value = 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
}
e.target.value = e.target.value.replace(/ /g, '?')
let data = []
data = e.target.value.split('').filter((i) => i === '?')
if (data.length > rangeOfBlankTile) {
e.target.value = e.target.value.replace(/\?$/, '')
}
})
var theSelect = document.getElementById('select_dropDown')
document.querySelector('.select_dropDown2').value = dictonary
const getDiff = (text1, text2) => {
var diffRange = []
var currentRange = undefined
for (var i = 0; i < text1.length; i++) {
if (text1[i] != text2[i]) {
if (currentRange == undefined) {
currentRange = [i]
}
}
if (currentRange != undefined && text1[i] == text2[i]) {
currentRange.push(i)
diffRange.push(currentRange)
currentRange = undefined
}
}
if (currentRange != undefined) {
currentRange.push(i)
diffRange.push(currentRange)
}
return diffRange
}
// getWords define...
const getData = async (serachValue) => {
try {
let selectedDictionary = document.querySelector('.select_dropDown2').value
main.innerHTML = `
`
/// loader
const response = await fetch(
`/.netlify/functions/getWords?name=${serachValue}&selecteddictionary=${selectedDictionary}`
)
const data = await response.json()
main.innerHTML = ''
getWords(data)
//getWords calling...
} catch (error) {
console.log(error)
}
}
//getData calling...
if (lengthValue === '1') {
errorMsg.innerHTML = 'words length should be more than 1'
} else {
getData(serachValue.toLowerCase())
}
// getWords function define...
function getWords(data) {
if (typeof data === 'string') {
errorMsg.innerHTML = 'no words found'
wordCount.innerHTML = ` 0 words with letters ${serachValue.split(
''
)}`
} else {
let newWordsLength = 0
// sort eventlistener
theSelect.addEventListener('change', () => {
sortValue = theSelect[theSelect.selectedIndex].text
if (sortValue == 'Z-A') {
sortBool = true
sortby(sortBool, data)
} else {
sortBool = false
sortby(sortBool, data)
}
if (sortValue == 'Points') {
sortBool = true
sortPointsby(sortBool, data)
}
})
for (let i = serachValue.length; i >= 1; i--) {
let newdata = data.filter((item) => item.length === i)
if (prefixValue) {
newdata = newdata.filter((item2) =>
item2.startsWith(prefixValue.toLowerCase())
)
startsWith.classList.add('tick')
startsWith.value = prefixValue
}
if (containsValue) {
newdata = newdata.filter((item) =>
item.includes(containsValue.toLowerCase())
)
mustInclude.classList.add('tick')
mustInclude.value = containsValue
}
if (suffixValue) {
newdata = newdata.filter((item) =>
item.endsWith(suffixValue.toLowerCase())
)
endsWith.classList.add('tick')
endsWith.value = suffixValue
}
if (exculdeValue) {
let data = []
newdata.map((item) => {
let check = false
for (let e = 0; e < exculdeValue.length; e++) {
const element = exculdeValue[e]
if (item.includes(element)) {
check = true
break
} else {
check = false
}
}
if (check === false) {
data.push(item)
}
})
exculdeWith.classList.add('tick')
exculdeWith.value = exculdeValue
newdata = data
}
if (includeValue) {
let data = []
newdata.map((item) => {
let check = false
for (let e = 0; e < includeValue.length; e++) {
const element = includeValue[e]
if (!item.includes(element)) {
check = true
break
} else {
check = false
}
}
if (check === false) {
data.push(item)
}
})
inculdeWith.classList.add('tick')
inculdeWith.value = includeValue
newdata = data
}
if (lengthValue) {
newdata = newdata.filter((item) => item.length == lengthValue)
wordLength.classList.add('tick')
wordLength.value = lengthValue
}
if (newdata.length === 0) {
main.innerHTML += ''
} else {
newWordsLength += newdata.length
const result = newdata.map((item) => {
var text1 = serachValue.replace('?', '')
var text2 = item
var text3 = item
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 += `${itemValue}`
}
})
if (item.length === 1) {
ok = false
newWordsLength = newWordsLength - 1
} else {
let ScrabbleLetterScore = ScrabbleScore()
sum = 0
item = item.toLowerCase()
for (let i = 0; i < item.length; i++) {
sum += ScrabbleLetterScore[item[i]] || 0 // for unknown characters
}
return `