-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
113 lines (94 loc) · 3.1 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
const operators = document.querySelectorAll('.operator');
const numberInput = document.querySelectorAll('.number');
const history = document.querySelector('.history');
const result = document.querySelector('.result');
const clear = document.querySelector('.clear');
const del = document.querySelector('.delete');
window.addEventListener('keydown', (e) => {
if(e.key === 'Backspace') result.textContent = result.textContent.slice(0,-1);
else if(e.key >=0 && e.key <=9) result.textContent += e.key;
else if(e.key === '.' && !result.textContent.includes('.')) result.textContent += e.key;
else if(e.key === '+' || e.key === '-' || e.key === '=') calculate(e.key);
else if(e.key === '*') calculate('x');
else if(e.key === '/') calculate('÷');
})
numberInput.forEach(number => {
number.addEventListener('click', () => {
if(number.textContent === '.' && result.textContent.includes('.')) return
result.textContent += number.textContent;
})
});
operators.forEach(operator => {
operator.addEventListener('click', () => {
if (result.textContent === '') return
if (history.textContent !== '') {
calculate(operator.textContent);
}
else {
history.textContent = result.textContent + ' ' + operator.textContent;
result.textContent = ''
}
})
});
clear.addEventListener('click', () => {
history.textContent = '';
result.textContent = '';
})
del.addEventListener('click', () => {
result.textContent = result.textContent.slice(0,-1);
})
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) {
if (b !== 0) return a / b;
alert('cannot divide by zero');
return a;
}
function operate(a, op, b) {
switch (op) {
case '+':
a = add(a, b);
break;
case '-':
a = subtract(a, b);
break;
case 'x':
a = multiply(a, b);
break;
case '÷':
a = divide(a, b);
break;
default:
break;
}
return res = parseFloat(Math.round(a * 10000) / 10000);
}
function calculate(operatorType) {
if (history.textContent !== '') {
if (operatorType === '=') {
firstNumber = parseFloat(history.textContent.slice(0,-1));
operation = history.textContent.slice(-1);
secondNumber = parseFloat(result.textContent);
result.textContent = operate(firstNumber, operation, secondNumber).toString();
history.textContent = '';
}
else {
firstNumber = parseFloat(history.textContent.slice(0,-1));
operation = history.textContent.slice(-1);
secondNumber = parseFloat(result.textContent);
history.textContent = operate(firstNumber, operation, secondNumber).toString() + ' ' + operatorType;
result.textContent = '';
}
}
else {
history.textContent = result.textContent + ' ' + operatorType;
result.textContent = ''
}
}