This repository has been archived by the owner on Dec 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mathTricks.js
106 lines (76 loc) · 2.43 KB
/
mathTricks.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
var numQuestions;
// window.onload = () => {
var numQuestions = Number(prompt("Enter the number questions you want to answer "));
// }
var operators = new Array(['+','-','*','/']);
function ndecimalPlaces(num,n){
whole = Math.floor(num);
rem = num- whole;
dp = round(Math.pow(10,n) * rem) / Math.pow(10,n);
return whole+dp;
}
var add = (num1,num2) => {
return num1 + num2;
}
var subtract = (num1,num2) => {
return num1 - num2;
}
var multiply = (num1,num2) => {
return num1*num2;
}
var divide = (num1,num2) => {
val = ndecimalPlaces(num1/num2,3);
return val;
}
while(true){
var i = 0;
var score = 0;
while(i < numQuestions){
firstNum = Math.round(Math.random()*100);
secondNum = Math.round(Math.random*100);
opNum = round(Math.random()*3);
num1Tag.innerText = String(firstNum);
opNumTag.innerText = operators[opNum];
opNumTag.innerText = '+';
num2Tag.innerText = String(secondNum);
button.addEventListener('click',() => {
var num1Tag = document.getElementById('num1');
var opNumTag = document.getElementById('operator');
var num2Tag = document.getElementById('num2');
var button = document.getElementById('button');
var answer = document.getElementById('answer');
firstNum = Math.round(Math.random()*100);
secondNum = Math.round(Math.random*100);
opNum = round(Math.random()*3);
num1Tag.innerText = String(firstNum);
opNumTag.innerText = operators[opNum];
// opNumTag.innerText = '+';
num2Tag.innerText = String(secondNum);
var solution = answer.value;
funcJson = {
'+' : add(firstNum+secondNum),
'-' : subtract(firstNum,secondNum),
'/' : divide(firstNum,secondNum),
'*' : multiply(firstNum,secondNum)
}
var result = funcJson[operators[opNum]];
// var result = add(firstNum,secondNum);
if(solution === result){
score++;
}
else {
alert(`Incorrect. Correct answer: ${result}`);
}
i += 1;
});// var operators = new Array(['+','-','*','/']);
alert(`You scored ${score}/${numQuestions}`);
var boolRes = confirm("Do you want to try again y/n ");
if(boolRes == true){
continue;
}
else {
window.close();
break;
}
}
}