-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
138 lines (133 loc) · 4 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
const body = document.querySelector("body");
const firstNumText = document.querySelector("#first-num");
const operatorText = document.querySelector("#operator");
const secondNumText = document.querySelector("#second-num");
const btns = document.querySelectorAll(".btn");
const incrLevelBtn = document.querySelector("#increment");
const decrLevelBtn = document.querySelector("#decrement");
const scoreText = document.querySelector("#score");
const hiscoreText = document.querySelector("#hiscore");
const levelText = document.querySelector("#level");
let hiscore = 0;
let score = 0;
let level = 10;
const operators = ["+", "-", "x"];
let correctAns = setQuestion();
initialize();
function initialize() {
btns.forEach((btn) => {
btn.addEventListener("click", btnClicked);
});
incrLevelBtn.addEventListener("click", incrLevel);
decrLevelBtn.addEventListener("click", decrLevel);
}
function incrLevel() {
const currLevel = parseInt(levelText.textContent);
if (currLevel < 3) {
level = level * 10;
levelText.textContent = currLevel + 1;
restart();
}
}
function decrLevel() {
const currLevel = parseInt(levelText.textContent);
if (currLevel > 1) {
level = Math.floor(level / 10);
levelText.textContent = currLevel - 1;
restart();
}
}
function setQuestion() {
let firstNum = Math.floor(Math.random() * level);
let secondNum = Math.floor(Math.random() * level);
const operator = operators[Math.floor(Math.random() * operators.length)];
const wrongAnswers = new Set();
let correctAns = null;
if (operator === "+") {
correctAns = firstNum + secondNum;
while (wrongAnswers.size < 4) {
const wrongAns =
Math.floor(Math.random() * level) + Math.floor(Math.random() * level);
if (wrongAns !== correctAns) {
wrongAnswers.add(wrongAns);
}
}
} else if (operator === "-") {
secondNum = Math.floor(Math.random() * firstNum);
correctAns = firstNum - secondNum;
while (wrongAnswers.size < 4) {
const wrongAns =
Math.floor(Math.random() * level) + Math.floor(Math.random() * level);
if (wrongAns !== correctAns) {
wrongAnswers.add(wrongAns);
}
}
} else if (operator === "x") {
if (level > 100) {
firstNum = Math.floor(Math.random() * 100);
secondNum = Math.floor(Math.random() * 100);
while (wrongAnswers.size < 4) {
const wrongAns =
Math.floor(Math.random() * 21) * Math.floor(Math.random() * 10);
if (wrongAns !== correctAns) {
wrongAnswers.add(wrongAns);
}
}
} else if (level > 10) {
firstNum = Math.floor(Math.random() * 21);
secondNum = Math.floor(Math.random() * 10);
while (wrongAnswers.size < 4) {
const wrongAns =
Math.floor(Math.random() * 21) * Math.floor(Math.random() * 10);
if (wrongAns !== correctAns) {
wrongAnswers.add(wrongAns);
}
}
}
while (wrongAnswers.size < 4) {
const wrongAns =
Math.floor(Math.random() * 21) * Math.floor(Math.random() * 10);
if (wrongAns !== correctAns) {
wrongAnswers.add(wrongAns);
}
}
correctAns = firstNum * secondNum;
}
operatorText.textContent = operator.toString();
firstNumText.textContent = firstNum.toString();
secondNumText.textContent = secondNum.toString();
const correctbtn = Math.floor(Math.random() * 4);
for (let i = 0; i < btns.length; i++) {
if (i === correctbtn) {
btns[i].textContent = correctAns;
} else {
btns[i].textContent = Array.from(wrongAnswers)[i];
}
}
return correctAns;
}
function btnClicked() {
checkAnswer(this);
update();
}
function checkAnswer(btn) {
if (btn.textContent == correctAns) {
const currLevel = parseInt(levelText.textContent);
score += currLevel;
if (score > hiscore) {
hiscore = score;
}
} else {
score = 0;
}
}
function update() {
scoreText.textContent = score;
hiscoreText.textContent = hiscore;
correctAns = setQuestion();
}
function restart() {
hiscore = 0;
score = 0;
update();
}