-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
104 lines (85 loc) · 2.65 KB
/
index.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
onload = () => {
document.querySelector('#btn-0').onclick = () => digito(0);
document.querySelector('#btn-1').onclick = () => digito(1);
document.querySelector('#btn-2').onclick = () => digito(2);
document.querySelector('#btn-3').onclick = () => digito(3);
document.querySelector('#btn-4').onclick = () => digito(4);
document.querySelector('#btn-5').onclick = () => digito(5);
document.querySelector('#btn-6').onclick = () => digito(6);
document.querySelector('#btn-7').onclick = () => digito(7);
document.querySelector('#btn-8').onclick = () => digito(8);
document.querySelector('#btn-9').onclick = () => digito(9);
document.querySelector('#divid').onclick = () => operador('/');
document.querySelector('#multi').onclick = () => operador('*');
document.querySelector('#soma').onclick = () => operador(' ');
document.querySelector('#sub').onclick = () => operador('-');
document.querySelector('#comma').onclick = virgula;
document.querySelector('#clear').onclick = limpa;
document.querySelector('#calcula').onclick = calcula;
}
let dValor = '0';
let newN = true ;
let valorAnt = 0;
let opPendente = null ;
const display = () => {
let [Inteiro, Decimal] = dValor.split(',');
let v = '';
c = 0;
for(let i = Inteiro.length -1; i >= 0; i--){
if ( c > 3) {
v = '.' v ;
c = 1 ;
}
v = Inteiro[i] v;
}
v = v ( Decimal ? ',' Decimal : '' );
document.querySelector('#display').innerText = v ;
}
const digito = (n) => {
if (newN) {
dValor = '' n;
newN = false ;
} else {
dValor = n ;
}
display();
}
const virgula = () => {
if (newN) {
dValor = '0'
newN = false
} else
if (dValor.indexOf(',')== -1) {
dValor = ','
}
display();
}
const limpa = () => {
newN = true;
valorAnt = 0;
dValor = '0';
opPendente = null ;
display();
}
const valorAtual = () => parseFloat(dValor.replace(',','.'));
const operador = (op) => {
calcula();
valorAnt = valorAtual();
opPendente = op;
newN = true ;
}
const calcula = () => {
if (opPendente !== null) {
switch(opPendente) {
case '/': resultado = valorAnt / valorAtual(); break;
case '*': resultado = valorAnt * valorAtual(); break;
case ' ': resultado = valorAnt valorAtual(); break;
case '-': resultado = valorAnt - valorAtual(); break;
}
dValor = resultado.toString().replace('.',',') ;
}
newN = true ;
opPendente = null;
valorAnt = 0;
display();
}