-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
116 lines (98 loc) · 3.21 KB
/
Copy pathscript.js
File metadata and controls
116 lines (98 loc) · 3.21 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
const result = document.querySelector(".result");
const buttons = document.querySelectorAll(".buttons button");
let currentNumber = "";
let firstOperand = null;
let operator = null;
let restart = false;
//atualiza o resultado para , senão, pega o número atual
function updateResult(originClear = false){
result.innerText = originClear ? 0 : currentNumber.replace(".", ",");
}
function addDigit(digit){
if (digit == "," && (currentNumber.includes(",") || !currentNumber)) //verifica se está clicando no botão da vírgula
return;
if (restart){
currentNumber = digit;
restart = false;
} else {
currentNumber += digit;
}
updateResult(); // atualiza o número em tela
}
function setOperator(newOperator){
if (currentNumber){
calculate();
firstOperand = parseFloat(currentNumber.replace(",", "."));
currentNumber = "";
}
operator = newOperator;
}
function calculate() {
if (operator == null || firstOperand == null) return;
let secondOperand = parseFloat(currentNumber.replace(",", "."));
let resultValue;
switch (operator){
case "+":
resultValue = firstOperand + secondOperand;
break;
case "-":
resultValue = firstOperand - secondOperand;
break
case "x":
resultValue = firstOperand * secondOperand;
break
case "÷":
resultValue = firstOperand / secondOperand;
break;
default:
return;
}
if (resultValue.toString().split(".")[1]?.length > 5){
currentNumber = parseFloat(resultValue.toFixed(5)).toString();
} else {
currentNumber = resultValue.toString();
}
operator = null;
firstOperand = null;
restart = true;
percentageValue = null;
updateResult();
}
function clearCalculator(){
currentNumber = "";
firstOperand = null;
operator = null;
updateResult(true);
}
function setPercentage(){
let result = parseFloat(currentNumber) / 100;
if (["+", "-"].includes(operator)){
result = result * (firstOperand || 1);
}
if (result.toString().split(".")[1]?.length > 5){ // verifica se a casa decimal é muito grande
result = result.toFixed(5).toString();
}
currentNumber = result.toString();
updateResult();
}
buttons.forEach((button) => {
button.addEventListener("click", () =>{
const buttonText = button.innerText; // variável que pega o texto do botao
if (/^[0-9,]+$/.test(buttonText)){ // Verifica o botao com o regex e testa
addDigit(buttonText); // chama a função para adicionar o texto do botao
} else if (["+", "-", "x", "÷"].includes(buttonText)){ // verifica se o botão do click é um dos operadores
setOperator(buttonText);
} else if (buttonText == "="){
calculate();
} else if (buttonText == "C") {
clearCalculator();
} else if (buttonText == "±"){
currentNumber = (
parseFloat(currentNumber || firstOperand) * -1
).toString();
updateResult();
} else if (buttonText == "%"){
setPercentage();
}
});
});