-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogical.js
150 lines (138 loc) · 3.96 KB
/
logical.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
//------Screen calulator------------
const display = document.getElementById('display--val');
//---Key types-----------------------------------------
const numbs = document.getElementsByClassName('numb');
const operators = document.getElementsByClassName('operator');
const reset = document.getElementById('reset');
const dec = document.getElementById('dec');
const del = document.getElementById('del');
// numbers for binary operations
let operand_a = 0, operand_b = 0, operation='',result= '';
//------numbers keys--------------------
for(let i=0; i< numbs.length; i++){
numbs[i].addEventListener('click', () =>{
// no 0 , no result and no error msg
if (display.innerHTML !== '0' && parseFloat(display.innerHTML) !== result && !isNaN(display.innerHTML)){
display.innerHTML +=numbs[i].id;
result = '';
} else{
display.innerHTML = numbs[i].id;
}
})
}
//-----------RESET Key---------------
reset.addEventListener('click', () =>{
display.innerHTML = '0';
operand_a = 0;
operand_b = 0;
operation = '';
result = '';
})
//------Decimal key-------------------
dec.addEventListener('click', () =>{
let str = display.innerHTML;
if(str.indexOf('.') == -1 && display.innerHTML !== result){
display.innerHTML += '.';
}
});
//-------Del key--------------
del.addEventListener('click', () =>{
let digit = display.innerHTML;
let cut = digit.length-1;
if(digit.length>1){
digit = digit.slice(0, cut);
display.innerHTML = digit;
} else if (digit.length = 1){
display.innerHTML = '0';
}
});
//-----Operators keys----------------
for(let i=0; i<operators.length; i++){
operators[i].addEventListener('click', () => {
// save the first operand and choose the operation
switch(operators[i].id){
case 'plus':
{
operand_a = parseFloat(display.innerHTML);
display.innerHTML='0';
operation = 'add';
}
break;
case 'minus':
{
operand_a = parseFloat(display.innerHTML);
display.innerHTML='0';
operation = 'substract';
}
break;
case 'divide':
{
operand_a = parseFloat(display.innerHTML);
display.innerHTML='0';
operation = 'divide';
}
break;
case 'mul':
{
operand_a = parseFloat(display.innerHTML);
display.innerHTML='0';
operation = 'multiply';
}
break;
case 'equal':
{
if(operand_a && !operand_b){
operand_b = parseFloat(display.innerHTML);
//result='';
}
finalResult();
}
break;
}
});
}
// Lets go!
function finalResult (){
switch (operation){
case 'add':
{
result = operand_a + operand_b;
display.innerHTML = result;
operand_a = 0;
operand_b = 0;
operation='';
}
break;
case 'substract':
{
result = operand_a - operand_b;
display.innerHTML = result;
operand_a = 0;
operand_b = 0;
operation='';
}
break;
case 'divide':
{
if(operand_b !== 0){
result = operand_a / operand_b;
display.innerHTML = result;
operand_a = 0;
operand_b = 0;
operation = '';
}else{
display.innerHTML = 'Error = divide by zero';
}
}
break;
case 'multiply':
{
result = operand_a * operand_b;
display.innerHTML = result;
operand_a = 0;
operand_b = 0;
operation = '';
}
break;
}
}