-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript.js
More file actions
153 lines (129 loc) · 4.27 KB
/
javascript.js
File metadata and controls
153 lines (129 loc) · 4.27 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
const add = function(n1, n2) {
return n1 + n2;
};
const subtract = function(n1, n2) {
return n1 - n2;
};
const multiply = function(n1, n2) {
return n1 * n2;
};
const divide = function(n1, n2) {
return n1 / n2;
};
const operate = function(operator, n1, n2){
if (operator === '+'){
return add(n1, n2);
} else if (operator === '-'){
return subtract(n1, n2);
} else if (operator === '*'){
return multiply(n1, n2);
} else if (operator === '/'){
return divide(n1, n2);
} else {
return 'ERROR';
}
}
console.log(operate("/", 2, 3));
const display = document.querySelector("#display");
const v1 = document.querySelector("#v1");
const op = document.querySelector("#op");
const v2 = document.querySelector("#v2");
const buttons = document.querySelectorAll("button");
const result = document.querySelector("#result");
let numberOne = "";
let numberTwo = "";
let operatorSimbol = "";
let resultValue = "";
const numbers = ["0", "1", "2","3", "4", "5", "6", "7", "8", "9"];
const operators = ["+", "-", "/", "*"];
const remove = ["⌫"];
const clearButton = ["clear"];
const equal = ["="];
const dot = ["."];
v2.textContent = "";
v1.textContent = "";
op.textContent = "";
function input(event){
if (operatorSimbol === "" && numbers.includes(event.target.textContent)){ // first value
numberOne = numberOne + event.target.textContent;
v1.textContent = numberOne;
}
if (numbers.includes(event.target.textContent) && resultValue != ""){
clear();
resultValue = "";
numberOne = numberOne + event.target.textContent;
v1.textContent = numberOne;
} // clean everything if a new number is typed before an operator, this, after the result is showed
if (operatorSimbol != "" && numbers.includes(event.target.textContent)){ // second value
numberTwo = numberTwo + event.target.textContent;
v2.textContent = numberTwo;
}
if (event.target.textContent === "="){ // =
let operacao = operate(operatorSimbol, Number(numberOne), Number(numberTwo));
resultValue = operacao;
result.textContent = operacao;
}
if (operators.includes(event.target.textContent) && resultValue){
numberTwo = "";
v2.textContent = numberTwo;
numberOne = resultValue;
v1.textContent = numberOne;
operatorSimbol = event.target.textContent;
op.operatorSimbol;
result.textContent = ""; // display difference
resultValue = "";
}
if (event.target.textContent === "clear"){
clear();
}
if (operators.includes(event.target.textContent)){
operatorSimbol = event.target.textContent;
op.textContent = operatorSimbol;
}
if (remove.includes(event.target.textContent)){
if (v2.textContent != ""){
numberTwo = numberTwo.slice(0, numberTwo.length - 1);
v2.textContent = numberTwo;
} else if (op.textContent != ""){
operatorSimbol = "";
op.textContent = operatorSimbol;
} else if (v1.textContent != ""){
numberOne = numberOne.slice(0, numberOne.length - 1);
v1.textContent = numberOne;
}
}
if (dot.includes(event.target.textContent)){
if (numberOne != "" && !numberOne.includes(dot) && numberTwo === ""){
numberOne = numberOne + ".";
v1.textContent = numberOne;
}
if (numberTwo != "" && !numberTwo.includes(dot)){
numberTwo = numberTwo + ".";
v2.textContent = numberTwo;
}
}
if (operators.includes(event.target.textContent) && numberTwo != ""){
let operacao = operate(operatorSimbol, Number(numberOne), Number(numberTwo));
operatorSimbol = event.target.textContent;
numberOne = operacao;
v1.textContent = numberOne;
op.textContent = operatorSimbol;
numberTwo = "";
v2.textContent = numberTwo;
}
}
function clear(){
numberOne = "";
numberTwo = "";
operatorSimbol = "";
v1.textContent = "";
v2.textContent = "";
op.textContent = "";
result.textContent = "";
}
buttons.forEach(button => {
button.addEventListener("click", input);
});
buttons.forEach(button => {
button.addEventListener("keydown", input);
})