-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.js
169 lines (146 loc) · 3.35 KB
/
calculator.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
var calculator = (function(){
//calculation
var calc = [];
var allTypes = ['+','/','%','^','*'];
var dots = 0;
var calcString = 'Enter your calculation here';
var digits = '';
var addingNumber = true;
var numberAfterDot = true;
var backspaceNumber = false;
var deleteNumber = false;
//DOM elements
var calcScreen = document.getElementsByClassName('calculation')[0].querySelector('p');
var numbers = document.getElementsByClassName('numbers');
var signs = document.getElementsByClassName('sign');
var reset = document.getElementsByClassName('reset');
var result = document.getElementsByClassName('result');
var equal = document.getElementsByClassName('equal');
var dot = document.getElementsByClassName('dot');
var del = document.getElementsByClassName('del');
_render();
//events
for(var i = 0; i<numbers.length; i++){
numbers[i].addEventListener('click',addNumber);
}
for(var i = 0; i<signs.length; i++){
signs[i].addEventListener('click',addSign);
}
reset[0].addEventListener('click',clear);
equal[0].addEventListener('click',findResult);
dot[0].addEventListener('click',addDot);
del[0].addEventListener('click',delNum);
//main
function addNumber(){
numberAfterDot = true;
var number = this.querySelector('p').innerHTML;
if(calcString == 'Enter your calculation here'){
calcString = '';
}
digits+=number;
calcString+= number;
_render();
deleteNumber = false;
}
function addSign(){
if(digits!='' && numberAfterDot){
if(!deleteNumber){
calc.push( digits );
}
var sign = this.querySelector('p').innerHTML;
calc.push( sign );
calcString+=sign;
digits = '';
_render();
}
deleteNumber = true;
}
function delNum(){
if(!deleteNumber){
calc.push( digits );
}
calcString = String(calcString);
calcString = calcString.slice(0,-1);
if( allTypes.includes( calc[calc.length - 1] ) && digits == ''){
calc.splice(-1 , 1);
digits = calc[calc.length - 1];
}
else{
digits = String(digits);
digits = digits.slice(0,-1);
if(digits !=''){
calc.splice(-1 , 1);
calc.push(digits);
}
else{
calc.splice(-1,1);
}
}
deleteNumber = true;
_render();
}
function addDot(){
dots++;
calcString+='.';
digits+='.';
numberAfterDot = false;
_render();
}
function clear(){
numberAfterDot = true;
calc = [];
calcLen = 0;
calcString = 'Enter your calculation here';
digits = '';
addingNumber = true;
result[0].innerHTML = '';;
_render();
}
function findResult(){
calc.push(digits);
while(calc.length > 1){
calc[0] = get_result(calc[0],calc[1],calc[2]);
calc.splice(1,2);
}
result[0].innerHTML = calc[0];
digits = calc[0];
calcString = calc[0];
calc.splice(0,1);
_render();
}
function get_result(x,sign,y){
if(sign == '+'){
return add(parseFloat(x),parseFloat(y));
}
if(sign == '-'){
return minus(parseFloat(x),parseFloat(y));
}
if(sign == '*'){
return mult(parseFloat(x),parseFloat(y));
}
if(sign == '/'){
return dia(parseFloat(x),parseFloat(y));
}
if(sign == '^'){
return power(parseFloat(x),parseFloat(y));
}
}
function add(x,y){
return x+y;
}
function power(x,y){
return Math.pow(x,y);
}
function dia(x,y){
return x/y;
}
function mult(x,y){
return x*y;
}
function minus(x,y){
return x - y;
}
function _render(){
calcScreen.innerHTML = calcString;
}
}());