-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexam.html
154 lines (140 loc) · 4.32 KB
/
exam.html
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
<!DOCTYPE >
<html>
<head>
<meta charset="UTF-8" />
<meta
name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1, maximum-scale=1"
/>
<title>javascript coding exam</title>
<link rel="stylesheet" href="css/bootstrap.min.css" />
</head>
<body>
<div class="container">
<h2>UIT Team JavaScript exam</h2>
<form class="form-horizontal" role="form">
<div class="form-group">
<button
onclick="arrangeRandomelyAfterEveryTwoSec()"
id="start"
type="button"
class="btn btn-default"
>
start random
</button>
<button
onclick="stopRandomTimer()"
id="stop"
type="button"
class="btn btn-default"
>
stop random
</button>
<button
onclick="sortTableList()"
id="sort"
type="button"
class="btn btn-default"
>
sort
</button>
</div>
</form>
<div class="contents">
<table class="table">
<thead>
<tr>
<th>Id</th>
<th>Image</th>
<th>Name</th>
<th>Price</th>
</tr>
</thead>
<tbody id="tbody"></tbody>
</table>
</div>
</div>
<script id="myJson" src="json/data.js"></script>
<script>
var tbody = document.getElementById("tbody");
const createRowAndDisplayResult = index => {
var tr = document.createElement("TR");
var th = document.createElement("TH");
th.setAttribute("id", "id" + index);
var th1 = document.createElement("TH");
var img = document.createElement("IMG");
img.setAttribute("id", "img" + index);
var th2 = document.createElement("TH");
th2.setAttribute("id", "name" + index);
var th3 = document.createElement("TH");
th3.setAttribute("id", "price" + index);
th1.appendChild(img);
tr.appendChild(th);
tr.appendChild(th1);
tr.appendChild(th2);
tr.appendChild(th3);
tbody.appendChild(tr);
document.getElementById("id" + index).innerHTML = TABLE_DATA[index].id;
document
.getElementById("img" + index)
.setAttribute("src", TABLE_DATA[index].thumbnailUrl);
if (index != 4) {
document.getElementById("name" + index).innerHTML =
TABLE_DATA[index].name;
} else {
document.getElementById("name" + index).innerHTML = "cony #10";
}
document.getElementById("price" + index).innerHTML =
TABLE_DATA[index].price;
};
</script>
<script>
var tbody = document.getElementById("tbody");
function getRandomArray(min, max) {
var nonRepetitiveRandomArray = [];
while (max >= min) nonRepetitiveRandomArray.push(max--);
nonRepetitiveRandomArray.sort(function() {
return 0.5 - Math.random();
});
return nonRepetitiveRandomArray;
}
function sortTableList() {
while (tbody.firstChild) {
tbody.removeChild(tbody.firstChild);
}
var sortedNumbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
for (var outerIndex = 0; outerIndex < TABLE_DATA.length; outerIndex++) {
for (var index = 0; index < TABLE_DATA.length; index++) {
if (sortedNumbers[outerIndex] == parseInt(TABLE_DATA[index].id)) {
createRowAndDisplayResult(index);
}
}
}
}
function arrangeRandomly() {
var randomArray = [];
while (tbody.firstChild) {
tbody.removeChild(tbody.firstChild);
}
randomArray = getRandomArray(0, 9);
randomArray.forEach(index => {
createRowAndDisplayResult(index);
});
}
for (var index = 0; index < TABLE_DATA.length; index++) {
createRowAndDisplayResult(index);
}
var repeat;
function arrangeRandomelyAfterEveryTwoSec() {
repeat = window.setInterval(() => {
arrangeRandomly();
}, 2000);
return repeat;
}
function stopRandomTimer() {
window.clearInterval(repeat);
}
</script>
<script src="js/app.js"></script>
</body>
</html>