-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.ts
203 lines (191 loc) · 6.74 KB
/
script.ts
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
const gens: string[][] = [
["A", "At", "a"],
["B", "b"],
["C", "chd", "chl", "ch", "c"],
["D", "d"],
["E", "Es", "En", "ej", "e"],
["M", "m"],
["V", "v"],
["W", "w"]
];
function loadPage() {
let box = document.getElementById("forms");
if (box != null) {
// FEMALE
let divElementFemale: HTMLDivElement = document.createElement("div");
divElementFemale.setAttribute("id", "femaleGens");
// Add text
let female: HTMLLabelElement = document.createElement("label");
female.appendChild(document.createTextNode("FEMALE"));
divElementFemale.appendChild(female);
divElementFemale.appendChild(document.createElement("br"));
// Generate selects
generateSelects(divElementFemale);
// MALE
let divElementMale: HTMLDivElement = document.createElement("div");
divElementMale.setAttribute("id", "maleGens");
// Add text
let male: HTMLLabelElement = document.createElement("label");
male.appendChild(document.createTextNode("MALE"));
divElementMale.appendChild(male);
divElementMale.appendChild(document.createElement("br"));
// Generate selects
generateSelects(divElementMale);
// Append to main div
box.appendChild(divElementMale);
box.appendChild(divElementFemale);
}
}
function generateSelects(divElement: HTMLDivElement): void {
gens.forEach(group => {
let usedGens: string[] = [];
let selectObject = document.createElement("select");
group.forEach(firstGen => {
group.forEach(secondGen => {
if (usedGens.includes(secondGen)) {
return;
}
let genCombo: string = firstGen + " " + secondGen;
let optionObject: HTMLOptionElement = document.createElement("option");
optionObject.setAttribute("value", genCombo);
optionObject.appendChild(document.createTextNode(genCombo));
selectObject.appendChild(optionObject);
});
usedGens.push(firstGen);
});
divElement.appendChild(selectObject);
divElement.appendChild(document.createElement("br"));
});
}
class FullGen {
gen: string;
amount: number;
constructor(gen:string, amount = 1) {
this.gen = gen;
this.amount = amount;
}
public get getAmount() : number {
return this.amount;
}
public incrementAmount(): void {
this.amount += 1;
}
}
function generate(): void {
let genM: string[] = [];
let genF: string[] = [];
let maleGens = document.getElementById("maleGens");
let femaleGens = document.getElementById("femaleGens");
if (maleGens == null || femaleGens == null) {
console.log("Male Gens or Female Gens are null");
return;
}
// Create arrays for
maleGens.childNodes.forEach(selectNode => {
if (selectNode instanceof HTMLSelectElement) {
genM.push(selectNode.value);
}
});
femaleGens.childNodes.forEach(selectNode => {
if (selectNode instanceof HTMLSelectElement) {
genF.push(selectNode.value);
}
});
// Create individual combos
let combinedGens: string[][] = [];
for (let i = 0; i < genM.length; i++) {
const splitMGens:string[] = genM[i].split(" ");
const splitFGens:string[] = genF[i].split(" ");
let combinedIndyGens: string[] = [];
splitMGens.forEach(maleGen => {
splitFGens.forEach(femaleGen => {
combinedIndyGens.push(maleGen+femaleGen);
});
});
combinedGens.push(combinedIndyGens);
}
// Create individual chances
if (combinedGens.length == 0) {
console.log("combinedGens are null");
return;
}
// Create combos
console.log("Creating combos");
let output: string[]= [];
createPairs("", combinedGens, 0, output);
const amount: number = output.length;
// Count and order
console.log("Counting and ordering combos");
const orderedCombos: FullGen[] = countAndOrderCombos(output);
// Display combos
console.log("Displaying");
const resultsDiv: HTMLElement = document.getElementById("resultsGrid");
let results: ChildNode[] = [];
orderedCombos.forEach(combo => {
let parentNode: HTMLDivElement = document.createElement("div");
// Gene
let geneNode: HTMLParagraphElement = document.createElement("p");
geneNode.setAttribute("class", "geneText");
geneNode.appendChild(document.createTextNode(combo.gen));
parentNode.appendChild(geneNode);
// Percentage
let percentageNode: HTMLParagraphElement = document.createElement("p");
percentageNode.setAttribute("class", "percentageText");
percentageNode.appendChild(document.createTextNode((100*(combo.getAmount/amount)).toString().substring(0,4) + "%"));
parentNode.appendChild(percentageNode);
results.push(parentNode);
});
resultsDiv.replaceChildren(...results);
// Change grid layout
const childAmount: number = resultsDiv.childNodes.length;
let columnAmount: number = 0;
if (childAmount%4 == 0) {
columnAmount = 4;
} else if (childAmount%3 == 0) {
columnAmount = 3;
} else if (childAmount%2 == 0) {
columnAmount = 2;
} else {
columnAmount = 1;
}
let columnStringAmount: string = "";
for (let i = 0; i < columnAmount; i++) {
columnStringAmount += " auto"
}
resultsDiv.style.gridTemplateColumns = columnStringAmount;
document.getElementById("results").style.display = "block";
}
function createPairs(inputString: string, allGens: string[][], i: number, output: string[]) {
for (let j = 0; j < allGens[i].length; j++) {
if (i == allGens.length - 1) {
output.push(inputString + allGens[i][j]);
continue;
}
createPairs(inputString + allGens[i][j] + " ", allGens, i + 1, output);
}
}
function countAndOrderCombos(combos: string[]): FullGen[] {
let fullGens: FullGen[] = [];
// Count
for (let i = 0; i < combos.length; i++) {
const combo = combos[i];
if (fullGens.length <= 0) {
fullGens.push(new FullGen(combo));
continue;
}
let contains: boolean = false;
fullGens.forEach(fullGen => {
if (fullGen.gen == combo) {
fullGen.incrementAmount();
contains = true;
}
});
if (!contains) {
console.log("Pushing new Fullgen(" + combo + ")");
fullGens.push(new FullGen(combo));
}
}
// Order
fullGens.sort((a,b) => a.getAmount - b.getAmount);
return fullGens;
}