-
Notifications
You must be signed in to change notification settings - Fork 0
/
draft.js
94 lines (83 loc) · 1.98 KB
/
draft.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
const display = document.querySelector('.item-header');
let oldnum;
let number;
let operator;
let result;
let isCalc = false;
function show(event) {
if (isCalc) {
display.textContent = null;
isCalc = false;
}
if (display.textContent.includes('.') &&
event.target.innerHTML === '.') {
display.innerHTML = display.innerHTML;
}
else if (!display.textContent && event.target.textContent === '.') {
display.textContent = '0.'
}
else {
display.innerHTML += event.target.innerHTML;
}
}
function clean(event) {
display.textContent = "";
isCalc = false;
}
function divide(number, event) {
if ((oldnum / number) % 1 !== 0) {
result = (oldnum / number).toFixed(8);
}
else {
result = (oldnum / number);
}
}
function sum(event) {
oldnum = parseFloat(display.textContent);
display.textContent = null;
operator = event.target.textContent;
}
function change(event) {
display.innerHTML = -display.innerHTML;
}
function sup(number) {
result = oldnum ** number;
}
function calculate(event) {
number = parseFloat(display.textContent);
display.innerHTML = null;
if (!oldnum) {
alert('Input number before action!');
return;
}
switch (operator) {
case "%" :
result = oldnum / 100 * number;
break;
case "/" :
divide(number);
break;
case "X" :
result = oldnum * number;
break;
case "-" :
result = oldnum - number;
break;
case "+" :
result = oldnum + number;
break;
case "Sup" :
sup(number);
break;
}
if (result === undefined || result === null) {
display.textContent = null;
}
else if (number === 0 && operator === '/') {
display.textContent = 'Not allowed';
}
else {
display.innerHTML = result;
isCalc = true;
}
}