Spaces:
Running
Running
File size: 3,982 Bytes
eb09d5f b0a47bb fbe3d46 eb09d5f b0a47bb eb09d5f b0a47bb eb09d5f fbe3d46 eb09d5f fbe3d46 eb09d5f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
let inWords = document
.getElementsByClassName("ct-codes-form")[0]
.getElementsByClassName("ct-code");
let codes = [];
let numCata = document.getElementsByClassName("ct-num-cata")[0].value;
let sortData;
initIU();
function submitData(e) {
const submitData = [];
sortData.forEach((order) => {
const simplePosition = {};
order.forEach((dataPosition) => {
simplePosition[dataPosition["code"]] = dataPosition["position"];
});
submitData.push(simplePosition);
});
const codesInput = document.createElement("input");
codesInput.type = "hidden";
codesInput.name = "sort_codes";
codesInput.value = JSON.stringify(submitData);
this.appendChild(codesInput);
}
function initIU() {
setCodes();
definePermutations();
addIUPermutations();
const formWords = document.getElementsByClassName("ct-codes-form")[0];
formWords.addEventListener("submit", submitData);
}
function setCodes() {
if (codes) {
codes = [];
}
for (let i = 0; i < inWords.length; i++) {
codes.push(inWords.item(i).value.toUpperCase());
}
}
function setPermutations() {
definePermutations();
addIUPermutations();
}
function definePermutations() {
setCodes();
const permutations = generatePermutations(codes);
const newPer = shuffle(permutations);
const usePermutations = [];
let index = 0;
while (true) {
if (usePermutations.length == numCata) {
break;
} else if (index < newPer.length) {
usePermutations.push(newPer[index]);
index++;
} else if (index >= newPer.length) {
index = 0;
}
}
const permutationsSort = [];
usePermutations.forEach((permutation) => {
let newValue = [];
let index = 1;
permutation.forEach((cod) => {
newValue.push({
code: cod,
position: index,
});
index++;
});
permutationsSort.push(newValue);
});
sortData = permutationsSort.slice();
}
function generatePermutations(objetos) {
var result = [];
var backtrack = (index, objetos) => {
if (index === objetos.length) {
result.push(objetos.slice());
return;
}
for (let j = index; j < objetos.length; j++) {
[objetos[index], objetos[j]] = [objetos[j], objetos[index]];
backtrack(index + 1, objetos);
[objetos[index], objetos[j]] = [objetos[j], objetos[index]];
}
};
backtrack(0, objetos);
return result;
}
function shuffle(arr) {
const arrSuffle = arr.slice();
for (let i = arrSuffle.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[arrSuffle[i], arrSuffle[j]] = [arrSuffle[j], arrSuffle[i]];
}
return arrSuffle;
}
/**
* Render the UI with permutations
*/
function addIUPermutations() {
const container = document.getElementsByClassName("ct-orden-list")[0];
const itemsChild = [];
container.replaceChildren();
let index = 1;
sortData.forEach((permuta) => {
const paragraph = document.createElement("p");
paragraph.classList.add("px-3", "font-bold", "text-xl", "text-black");
paragraph.textContent = `orden ${index}`;
const unlist = document.createElement("ul");
unlist.classList.add(
"flex",
"flex-wrap",
"justify-center",
"items-center",
"gap-3",
"px-3",
"py-1",
"text-white"
);
permuta.forEach((obj) => {
const itemList = document.createElement("li");
itemList.classList.add(
"px-2",
"bg-gray-600",
"font-bold",
"tracking-wide"
);
itemList.textContent = `${obj.position}: ${obj.code}`;
unlist.appendChild(itemList);
});
const section = document.createElement("section");
section.classList.add(
"bg-surface-card",
"py-2",
"sm:max-w-80",
"w-full",
"flex",
"flex-col",
"justify-center",
"items-center"
);
section.appendChild(paragraph);
section.appendChild(unlist);
container.appendChild(section);
index++;
});
}
|