-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
106 lines (87 loc) · 2.91 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
let fInput = document.getElementById('firstInput');
let sInput = document.getElementById('secondInput');
let operator = document.getElementById('operator');
let userAns = document.getElementById('userAns');
let showScore = document.getElementById('showScore');
let showNoCorrect = document.getElementById('showNoCorrect');
let showNoWrong = document.getElementById('showNoWrong');
let showResult = document.getElementById('showResult');
let ids = document.getElementById('showTime');
showScore.value = 0;
showNoCorrect.value = 0;
showNoWrong.value = 0;
let correctAns
// Choose operator
let arr = ["+", "-", "*", "/"]
randomNo = Math.floor(Math.random() * 4)
operator.value = arr[randomNo]
const getRandNum = (e) => {
if (arr[randomNo] == "+") {
value1 = Math.floor(Math.random() * 100);
value2 = Math.floor(Math.random() * 80);
}
else if (arr[randomNo] == "-") {
value1 = Math.floor(Math.random() * 100);
value2 = Math.floor(Math.random() * 50);
}
else if (arr[randomNo] == "*") {
value1 = Math.floor(Math.random() * 20);
value2 = Math.floor(Math.random() * 10);
}
else if (arr[randomNo] == "/") {
value1 = Math.floor(Math.random() * 20);
value2 = Math.floor(Math.random() * 5);
}
}
const calculate = (e) => {
if (userAns.value == correctAns) {
alert(`Your answer ${userAns.value} is Correct`);
showScore.value = parseInt(showScore.value) + 5;
showNoCorrect.value = parseInt(showNoCorrect.value) + 1;
let correctAudio = new Audio('Correct_Buzzer.m4a');
correctAudio.play();
}
else {
alert(`Your answer ${userAns.value} is Wrong. The correct ans is ${correctAns}`);
let wrongAudio = new Audio('Wrong_Buzzer.m4a');
wrongAudio.play();
showNoWrong.value = parseInt(showNoWrong.value) + 1;
showScore.value = parseInt(showScore.value) - 3;
}
fInput.value = " ";
sInput.value = " ";
userAns.value = "";
// value1 = Math.floor(Math.random()*50);
// value2 = Math.floor(Math.random()*50);
getRandNum();
randomNo = Math.floor(Math.random() * 4)
operator.value = arr[randomNo]
process();
showResult.value = " "
// timeOut();
};
const process = (e) => {
fInput.value = value1;
sInput.value = value2;
// correctAns = value1 + value2;
if (arr[randomNo] == "+") {
correctAns = value1 + value2;
}
else if (arr[randomNo] == "-") {
correctAns = value1 - value2;
}
else if (arr[randomNo] == "*") {
correctAns = value1 * value2;
}
else if (arr[randomNo] == "/") {
correctAns = value1 / value2;
}
};
document.getElementById("btn").addEventListener('click', calculate);
const helpAns = (e) => {
showResult.value = correctAns;
}
// setTimeout(timeOut , 1000)
document.getElementById('showHelp').addEventListener('click', helpAns);
getRandNum();
process();