-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
145 lines (119 loc) · 3.45 KB
/
index.js
File metadata and controls
145 lines (119 loc) · 3.45 KB
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
const h1Text = document.createElement("h1");
h1Text.textContent = "Odds and Events";
document.body.appendChild(h1Text);
const mainText = document.createElement("p");
mainText.textContent = "Add a number to the bank";
mainText.style.display = "inline";
const addNumberToBank = document.createElement("input");
addNumberToBank.type = "text";
const btnAddNumber = document.createElement("button");
btnAddNumber.textContent = "Add Number";
btnAddNumber.id = "add";
const btnSort1 = document.createElement("button");
btnSort1.textContent = "Sort 1";
btnSort1.id = "sort1";
const btnSortAll = document.createElement("button");
btnSortAll.textContent = "Sort All";
btnSortAll.id = "sortAll";
document.body.appendChild(mainText);
document.body.appendChild(addNumberToBank);
document.body.appendChild(btnAddNumber);
document.body.appendChild(btnSort1);
document.body.appendChild(btnSortAll);
const bankText = document.createElement("h2");
bankText.textContent = "Bank";
document.body.appendChild(bankText);
const bankBox = document.createElement("input");
bankBox.type = "text";
bankBox.id = "bank";
bankBox.style.width = "60%";
bankBox.readOnly = true;
document.body.appendChild(bankBox);
const oddsText = document.createElement("h2");
oddsText.textContent = "Odds";
document.body.appendChild(oddsText);
const oddsBox = document.createElement("input");
oddsBox.type = "text";
oddsBox.id = "odds";
oddsBox.style.width = "60%";
oddsBox.readOnly = true;
document.body.appendChild(oddsBox);
const evensText = document.createElement("h2");
evensText.textContent = "Evens";
document.body.appendChild(evensText);
const evensBox = document.createElement("input");
evensBox.type = "text";
evensBox.id = "evens";
evensBox.style.width = "60%";
evensBox.readOnly = true;
document.body.appendChild(evensBox);
btnAddNumber.addEventListener("click", () => {
const newNumber = addNumberToBank.value.trim();
if (newNumber === "" || isNaN(newNumber)) {
alert("Please enter a valid number.");
return;
}
if (bankBox.value === "") {
bankBox.value = newNumber;
} else {
bankBox.value += "," + newNumber;
}
addNumberToBank.value = "";
});
btnSort1.addEventListener("click", () => {
const bankValues = bankBox.value
.split(",")
.map((v) => v.trim())
.filter((v) => v !== "");
if (bankValues.length === 0) {
alert("The bank is empty!");
return;
}
const first = parseInt(bankValues.shift(), 10);
if (isNaN(first)) {
alert("The first item is not a valid number.");
return;
}
bankBox.value = bankValues.join(",");
if (first % 2 === 0) {
if (evensBox.value === "") {
evensBox.value = first;
} else {
evensBox.value += "," + first;
}
} else {
if (oddsBox.value === "") {
oddsBox.value = first;
} else {
oddsBox.value += "," + first;
}
}
});
btnSortAll.addEventListener("click", () => {
const bankValues = bankBox.value
.split(",")
.map((v) => v.trim())
.filter((v) => v !== "");
if (bankValues.length === 0) {
alert("The bank is empty!");
return;
}
for (let value of bankValues) {
const number = parseInt(value, 10);
if (isNaN(number)) continue;
if (number % 2 === 0) {
if (evensBox.value === "") {
evensBox.value = number;
} else {
evensBox.value += "," + number;
}
} else {
if (oddsBox.value === "") {
oddsBox.value = number;
} else {
oddsBox.value += "," + number;
}
}
}
bankBox.value = "";
});