-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
117 lines (105 loc) · 2.31 KB
/
main.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
116
117
const score = document.querySelector('.score')
const question = document.querySelector('.question span')
const answers = document.querySelector('.answers')
const restartGame = document.querySelector('.restartGame')
const datas = [
{
question: ['red fruit'],
answers: ['apple', 'banana', 'pear', 'lemon'],
trueAnswer: 'apple'
},
{
question: ['wheels number'],
answers: [1, 2, 3, 4],
trueAnswer: 4
},
{
question: ['eyes number'],
answers: [1, 2, 3, 4],
trueAnswer: 2
},
{
question: ['speed of sound (m/s)'],
answers: [343, 945, 453, 628, 424, 387],
trueAnswer: 343
},
{
question: ['color of water'],
answers: ['white', 'green', 'blue', 'none in this list'],
trueAnswer: 'none in this list'
}
]
let counter = 0
let scoreGame = 0
function deleteAnswers() {
if (counter < datas.length) {
for (let index = 0; index < answers.children.length; index) {
answers.removeChild(answers.children[0])
console.log('asdasd');
}
}
}
function createAnswers() {
if (counter < datas.length) {
datas[counter].answers.forEach(data => {
const li = document.createElement('li')
answers.appendChild(li)
li.textContent = data
});
}
return
}
function createQuestions() {
if (counter < datas.length) {
datas[counter].question.forEach(data => {
question.textContent = data
});
return
}
}
function verifyAnswer(e) {
if (e.target.textContent == trueAnswer()) {
scoreGame += 10
return true
}
scoreGame -= 10
e.target.classList.add('alert')
setTimeout(() => {
e.target.classList.remove('alert')
}, 500)
return false
}
function trueAnswer() {
return datas[counter].trueAnswer
}
function handleStepsGame() {
if (counter < datas.length) {
trueAnswer()
deleteAnswers()
createQuestions()
createAnswers()
return
}
answers.textContent = ''
question.textContent = 'End'
score.textContent = 'your score is: ' + scoreGame
return
}
answers.addEventListener('click', (e) => {
let verify = verifyAnswer(e)
console.log(counter);
if (verify) {
counter++
handleStepsGame()
}
return
})
restartGame.addEventListener('click', () => {
counter = 0
scoreGame = 0
question.textContent = ''
score.textContent = ''
handleStepsGame()
})
createQuestions()
createAnswers()