-
Notifications
You must be signed in to change notification settings - Fork 1
/
scripts.js
116 lines (98 loc) · 2.27 KB
/
scripts.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
let rT = 0;
let buF = "0";
let pF;
const screen = document.querySelector('.screen');
function ClickButton(value){
if (isNaN(value)){
handleSymbol(value);
}
else{
handleNumber(value);
}
// Check for the easter egg [WIP]
if (buF === '69=' && value === '=') {
screen.innerText = 'nice ;)';
return;
}
screen.innerText = buF;
}
//defining cases for the click of buttons like AC = and the arithmetic functions
function handleSymbol(symbol){
switch(symbol){
case 'AC':
buF = '0';
rT = 0;
break
case '=':
if (pF === null){
return;
}
doMath(parseInt(buF));
pF = null;
buF = rT;
rT = 0;
break;
case '+':
case '−':
case '×':
case '÷':
case '∧':
case '%':
handleMath(symbol);
break;
}
}
//sets the value of rT to intbuF,
//meaning that the current value of buF becomes the new running total.
function handleMath(symbol){
if (buF === '0'){
return;
}
const intbuF = parseInt(buF);
if (rT === 0){
rT = intbuF;
}
else{
doMath(intbuF);
}
pF = symbol;
buF = '0';
}
//does math functions using arithmetic operators
function doMath(intbuF){
if (pF === '+'){
rT += intbuF;
}
else if (pF === '−'){
rT -= intbuF;
}
else if (pF === '×'){
rT *= intbuF;
}
else if (pF === '÷'){
rT /= intbuF;
}
else if (pF === '∧'){
rT **= intbuF;
}
else if (pF === '%'){
rT %= intbuF;
}
}
function handleNumber(numberString){
if (buF === '0'){
buF = numberString;
}
else{
buF += numberString;
}
}
//function selects an element with a class of "calculator-button" using the query selector
//using an event listener for the "click" and the __init__ function is a constructor function for when the page loads
//making it active for listening and clicking of button.
function __init__ (){
document.querySelector('.calculator-button').addEventListener('click', function(event){
ClickButton(event.target.innerText);
})
}
__init__ ();