-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
117 lines (90 loc) · 3.9 KB
/
script.js
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
const current = document.getElementById('current');
const total = document.getElementById('total');
const question = document.getElementById('question');
const listItem = document.getElementById('listItem');
const btn = document.getElementById('btn');
btn.disabled = true
const start = document.getElementById('start')
const listSpan = (parrent, obj) => {
for (let i = 0; i < obj.length; i++) {
const span = document.createElement('span')
span.className = `circle`
parrent.append(span)
}
}
fetch('./questions.json')
.then((json) => json.json())
.then((res) => {
const mainObj = res
listSpan(document.getElementById('listCircle'), mainObj)
const listCircle = document.querySelectorAll('#listCircle')[0].childNodes
const [one, two, three] = listCircle
total.innerText = mainObj.length
const arrBool = []
const listBtn = document.querySelectorAll('#listItem')[0].childNodes
const render = () => {
current.innerText = btn.value
question.innerText = mainObj[btn.value - 1].question
mainObj[btn.value - 1].answers.map((item) => {
const divItem = document.createElement('button')
divItem.className = 'item'
divItem.innerText = item.text
listItem.append(divItem)
const colorCircle = () => {
switch (+btn.value) {
case 1:
one.classList.add(`${arrBool[0]}-circle`)
btn.disabled = false
break
case 2:
two.classList.add(`${arrBool[1]}-circle`)
btn.disabled = false
break
case 3:
three.classList.add(`${arrBool[2]}-circle`)
btn.disabled = false
break
}
}
const addEvent = (e) => {
item.isCorrect
? e.target.classList.add('true')
: e.target.classList.add('false')
arrBool.push(item.isCorrect)
colorCircle()
for (let j = 0; j < listBtn.length; j++) {
listBtn[j].disabled = true
}
if (arrBool.length === 3) {
btn.style.display = `none`
start.style.display = `block`
}
}
divItem.addEventListener('click', (e) => addEvent(e))
})
}
render()
btn.addEventListener('click', () => {
btn.value = +btn.value + 1
if (+btn.value >= 3) btn.value = 3
listItem.innerHTML = ''
btn.disabled = true
render()
})
start.addEventListener('click', () => {
const trueCount = arrBool.filter((bool) => bool === true)
const result = alert(`
Правильный ответов ${trueCount.length}
Не правильных ответов ${mainObj.length - trueCount.length}
`)
btn.value = 1
listItem.innerHTML = ''
arrBool.length = 0
btn.style.display = `block`
start.style.display = `none`
for (let k = 0; k < listCircle.length; k++) {
listCircle[k].className = 'circle'
}
render()
})
})