-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
75 lines (59 loc) · 2.17 KB
/
main.js
File metadata and controls
75 lines (59 loc) · 2.17 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
//Declarando variáveis
let numbers = []
let lastOperation = []
const result = document.getElementById('concatenatedNumber')
//Função limpar
cleanVisor = () => result.value = ""
//Exibe resultado no visor
showResult = (total) => result.value = total
//Número inserido pelo usuário
function insertNumber(eachNumber){
//Para limpar visor caso já exista um resultado sendo exibido
if (lastOperation[1] != undefined ){cleanVisor(), lastOperation.shift()}
//Concatena os números digitados
concatenatedNumber.value += eachNumber
userNumber = concatenatedNumber.value
console.log(userNumber)}
//Cálculos
function calculate(symbols){
console.log(symbols)
lastOperation[0] = symbols
lastOperation[1] = symbols
cleanVisor();
//Caso nenhum número seja inserido o valor no visor ficará o mesmo
if (userNumber == ''){return false}
//Alimenta visor últimos digitos
previousNumbers.value += symbols + userNumber
//Realizará os cálculos após o primeiro número ser inserido
if(numbers[0] >= 0){
numbers[1] = parseInt(userNumber)
total = math(numbers[0], numbers[1],symbols)
numbers[0] = total
userNumber = ''
console.log('Second Function',total)
showResult(total)}
//Primeira condição para inserir prmeiro número
if(numbers[0] == undefined){
numbers[0] = parseInt(userNumber)
console.log("First function")
cleanVisor()}
}
//Realiza o cálculo
function math(value1, value2,symbols){
if (symbols == '+'){return total = value1 + value2}
if (symbols == '-'){return total = value1 - value2}
if (symbols == '*'){return total = value1 * value2}
if (symbols == '/'){return total = value1 / value2}}
//Função armazém operação anterior
function operacanterior(){
if (lastOperation[0] != undefined){
strOperation = lastOperation[0]
lastOperation.shift()
console.log('function last operation:',strOperation)
return calculate(strOperation)}}
//Limpar calculadora
function cleanValues(){
numbers[0] = null
numbers[1] = null
previousNumbers.value = null
concatenatedNumber.value = null}