-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculadora.js
More file actions
121 lines (101 loc) · 3.38 KB
/
calculadora.js
File metadata and controls
121 lines (101 loc) · 3.38 KB
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
'use strict';
const display = document.getElementById('display');
const numeros = document.querySelectorAll('[id*=tecla]');
const operadores = document.querySelectorAll('[id*=operador]');
let novoNumero = true;
let operador;
let numeroanterior;
const operacaoPendente = () => operador !== undefined;
const calcular = () => {
if (operacaoPendente()) {
const numeroAtual = parseFloat(display.textContent.replace('.','').replace(',','.'));
novoNumero = true;
const resultado = eval('${numeroAnterior}${operador}${numeroAtual');
atualizarDisplay(resultado);
}
};
const atualizarDisplay = (texto) => {
if (novoNumero) {
display.textContent = texto.toLocaleString('BR');
novoNumero = false;
} else {
display.textContent += texto.toLocaleString('BR');
}
document.querySelector('#igual').focus();
};
const inserirNumero = (evento) => atualizarDisplay(evento.target.textContent);
numeroanterior.forEach((numero) => numero.addEventListener('click', inserirNumero ));
const selecionarOperador = (evento) => {
if (!novoNumero) {
calcular();
novoNumero = true;
operador = evento.target.textContent;
numeroAnterior = parseFloat(display.textContent.replace('.',''). replace(',','.'));
}
};
operadores.forEach((operador) =>
operador.addEventListener('click', selecionarOperador)
);
const ativarIgual = () => {
calcular();
operador = undefined
};
document.getElementById('igual').addEventListener('click', ativarIgual);
const limparDisplay = () => (display.textContent = '');
document.getElementById('limparDisplay').addEventListener('click', limparDisplay);
const limparCalculo = () => {
limparDisplay();
operador = undefined;
novoNumero = true;
numeroAnterior = undefined;
};
document.getElementById('limparCalculo').addEventListener('click', limparCalculo);
const removerUltimoNUmero = () =>
(display.textContent = display.textContent.slice(0, -1));
document.getElementById('backspace').addEventListener('click', removerUltimoNUmero);
const inverterSinal = () => {
novoNumero = true;
atualizarDisplay(display.textContent * -1);
}
document.getElementById('inverter').addEventListener('click', inverterSinal);
const existeDecimal = () => display.textContent.indexOf(',') !== -1;
const existeValor = () => display.textContent.length > 0;
const inserirDecimal = () => {
if (!existeDecimal()) {
if (novoNumero) {
atualizarDisplay('0, ');
} else {
atualizarDisplay(',');
}
}
};
document.getElementById('decimal').addEventListener('click',inserirDecimal);
const mapaTeclado = {
0: 'tecla0',
1: 'tecla1',
2: 'tecla2',
3: 'tecla3',
4: 'tecla4',
5: 'tecla5',
6: 'tecla6',
7: 'tecla7',
8: 'tecla8',
9: 'tecla9',
'/': 'operadorDividir',
'*': 'operadorMultiplicar',
'%': 'operadorPorcentagem',
'-': 'operadorSubtrair',
'+': 'operadorSoma',
'=': 'operadorIgual',
Enter: 'igual',
backspace: 'backssssspace',
c: 'limparDisplay',
Escape: 'limparCalculo',
',': 'decimal',
};
const mapearTeclado = (evento) => {
const tecla = evento.key;
const teclaPermitida = () => Object.keys(mapaTeclado).indexOf(tecla) !== -1;
if (teclaPermitida()) document.getElementById(mapaTeclado)[tecla].click();
};
document.addEventListener('keydown', mapearTeclado);