-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
146 lines (135 loc) · 5.43 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
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
// basic functions to perform arithmetic operations
function add(a,b) {
return a + b;
}
function subtract(a,b) {
return a - b;
}
function multiply(a,b) {
return a * b;
}
function divide(a,b) {
return a / b;
}
// function to perform operation based on given sign and two other values
function operate (sign, total, a) {
switch(sign) {
case '+':
total = add(total, a);
break;
case '-':
total = subtract(total, a);
break;
case '*':
total = multiply(total, a);
break;
case '/':
if (a == 0) { // when you try to divide using zero
error.textContent = 'oh oh are you trying to divide by zero?';
return total;
}
total = divide(total, a);
a = null;
break;
}
return Math.round(total * 1000) / 1000; // rounds off the given value to 2 decimals
}
const numbers = document.querySelectorAll('.numbers');
const operators = document.querySelectorAll('.operators');
const keys = document.querySelectorAll('.keys');
const display = document.querySelector('#display-screen');
const error = document.querySelector('#error');
let a = null, total = null, sign = '', prevSign = '', flag = true;
// calls function when any of the number button is clicked
numbers.forEach(number => {
number.addEventListener('click', () => {
if (flag == false) { // asks user to clear the screen because they clicked "equal to"
error.textContent = "Clear the screen!";
return;
}
if (display.textContent.length > 15 && a != null) { // limits the digits on screen below 15
error.textContent = "Sorry! I can't display more than 15 digits on screen, clear the screen";
return;
}
if (a == null) // if a is empty, create empty display screen to start getting input for a, else just append to a
display.textContent = '';
display.textContent += number.getAttribute('id');
a = (display.textContent.includes('.')) ? parseFloat(display.textContent) : parseInt(display.textContent);
});
});
// calls function when any of the operator is clicked
operators.forEach(operator => {
operator.addEventListener('click', () => {
sign = operator.getAttribute('id'); // gets the sign of operator
if (flag == false && sign != '=') { // checks whether equal to was pressed before
flag = true;
}
if (sign == '=' && prevSign == '=') { // when user clicks equal to more than once
error.textContent = "It's better if you don't click on '=' more than once.";
return;
}
if (sign == '=' && prevSign == '') { // clicks equal to before entering arithmetic operator
error.textContent = "Why don't you add some arithmetic operator before clicking '='?";
return;
}
if (a == null && prevSign != '=') { // clicks on operator more than once
error.textContent = "Don't forget to add numbers before clicking on operators!";
return;
}
if (prevSign) // removes the hold button effect
document.getElementById(prevSign).classList.remove('activated');
// does the calculation
if (total == null) {
total = a;
a = null;
display.textContent = total;
error.textContent = '';
} else {
error.textContent = '';
total = operate(prevSign, total, a);
a = null;
if(total.toString().length > 15) {
flag = 0;
error.textContent = "Sorry! I can't display more than 15 digits on screen, clear the screen";
return;
}
display.textContent = total;
}
prevSign = sign;
if (sign == '=') // to avoid users entering number after entering equal to
flag = 0;
else // adds hold effect on operators
operator.classList.add('activated');
});
});
// calls function when special keys are clicked
keys.forEach(key => {
key.addEventListener('click', () => {
let value = key.getAttribute('id');
if (value == 'sign') { // if sign button is clicked, changes the sign of number and display
if (a==null) {
total *= -1;
display.textContent = total;
return;
}
a *= -1;
display.textContent = a;
} else if (value == 'clear') { // clears everything as it mentions
flag = true;
a = null; total = null;
prevSign = '';
sign = '';
display.textContent = '';
error.textContent = '';
buttons.forEach(button => button.classList.remove('activated'));
} else if (value == 'delete') { // works like backspace
display.textContent = display.textContent.slice(0, -1);
a = (display.textContent.includes('.')) ? parseFloat(display.textContent) : parseInt(display.textContent);
} else if (value == 'decimal') { // for decimal value
if (display.textContent.includes('.'))
error.textContent = "Do you think you can have two decimal points?!?!";
else
display.textContent = display.textContent + '.';
}
});
});