-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript.js
More file actions
126 lines (100 loc) · 3.13 KB
/
javascript.js
File metadata and controls
126 lines (100 loc) · 3.13 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
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");
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));
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 = ["="];
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;
resultValue = "";
}
if (event.target.textContent === "clear"){
clear();
}
if (operators.includes(event.target.textContent)){
operatorSimbol = event.target.textContent;
op.textContent = operatorSimbol;
}
/* numberOne = operacao;
numberTwo = "";
operatorSimbol = "";
v1.textContent = numberOne;
v2.textContent = "";
op.textContent = "";
*/
}
function clear(){
numberOne = "";
numberTwo = "";
operatorSimbol = "";
v1.textContent = "";
v2.textContent = "";
op.textContent = "";
result.textContent = "";
}
buttons.forEach(button => {
button.addEventListener("click", input);
});