let main = document.querySelector('.main')
let tab_container = document.querySelector('.tab_container')
var sortValue
var sortBool = false
var theSelect = document.getElementById('select_dropDown')
let lengthSelect = document.querySelector(".length-select");
// Add Filtering
let sections = {}
function Filtering(id) {
id = lengthSelect.value
main.innerHTML += ``
var section = document.querySelectorAll('.allGroupWords')
var sort_val = "alpha";
Array.prototype.forEach.call(section, function (e) {
if (document.body.clientWidth > 991) {
sections[e.id] = e.offsetTop - 10
} else {
sections[e.id] = e.offsetTop - 10
}
})
console.log(sort_val + '_' + id);
document.documentElement.scrollTop = sections[sort_val + '_' + id] + 5
}
// sorting for words
theSelect.addEventListener("change", () => {
sortValue = theSelect[theSelect.selectedIndex].text;
let list_word = document.getElementsByClassName("list_word")
let data = []
Array.from(list_word).forEach(item => {
item.removeChild(item.lastElementChild);
data.push(item.innerHTML.replace(/\s/g, ''))
})
if (sortValue == "Z-A") {
sortBool = "Z-A";
sortby(sortBool, data);
} else if (sortValue == "Points") {
sortBool = "Points";
sortPointsby(sortBool, data);
} else {
sortBool = "A-Z";
sortby(sortBool, data);
}
});
// sort by aplhabets
function sortby(sortValue, data) {
if (sortValue === "Z-A") {
appendData(sortValue, data.reverse())
}
else if (sortValue === "A-Z") {
appendData(sortValue, data.sort())
}
}
// sorting by points
function sortPointsby(sortValue, data) {
if (sortValue === "Points") {
let newArray = [];
data.map((item) => {
if (item.length === 1) {
ok = false;
} else {
let ScrabbleLetterScore = ScrabbleScore();
let points = 0;
let word = item.replace(/<\/?[^>]+>/gi, '')
for (let i = 0; i < word.length; i++) {
points += ScrabbleLetterScore[word[i]] || 0;
}
const value = {
words: item,
points: points,
};
newArray.push(value);
}
});
newArray.sort(function (a, b) {
return b.points - a.points;
});
appendData(sortValue, newArray)
}
}
function appendData(sortValue, data) {
main.innerHTML = "";
let newWordsLength = 0;
for (let i = 15; i > 0; i--) {
let newdata
if (sortValue === "Points") {
newdata = data.filter((item) => item.words.replace(/<\/?[^>]+>/gi, '').length === i)
} else {
newdata = data.filter((item) => item.replace(/<\/?[^>]+>/gi, '').length === i)
}
if (newdata.length === 0) {
main.innerHTML += "";
} else {
newWordsLength += newdata.length;
const result = newdata.map((item) => {
let ScrabbleLetterScore = ScrabbleScore();
let points = 0;
let word
if (sortValue === "Points") {
word = item.words.replace(/<\/?[^>]+>/gi, '')
item = item.words
} else {
word = item.replace(/<\/?[^>]+>/gi, '')
}
for (let i = 0; i < word.length; i++) {
points += ScrabbleLetterScore[word[i]] || 0; // for unknown characters
}
return `