-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
157 lines (130 loc) · 5.99 KB
/
index.js
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
154
155
156
157
let calculation ={
firstNum: 0,
operator: null,
secondNum: 0,
result: 0,
toggle: false
}; // this is to store the calculation in the object
//--------------------- This is for typing a number ---------------------
const numberButton = document.querySelectorAll(".num_btn");
for(let i = 0; i < numberButton.length; i++){
numberButton[i].addEventListener("click", function(){
let num = numberButton[i].innerHTML;
const temp = document.querySelector(".display p").innerHTML;
const temp2 = document.querySelector(".display h6").innerHTML;
//this is for the first div on display
if( temp2.length === 0 || temp2 === "0" && temp.length === 1){
document.querySelector(".display h6").innerHTML = num;
}
if(temp == "0" && temp.length == 1){//for the main display
document.querySelector(".display p").innerHTML = num;
if(temp2.length > 1){//when pressed the 1st math operator the main display will be 0 and the h6 will add the 2nd num to it string instead
document.querySelector(".display h6").innerHTML += num;
}
}
else if (!calculation.toggle){ //fault by default
document.querySelector(".display p").innerHTML += num;
document.querySelector(".display h6").innerHTML += num;
}
else if(calculation.toggle == true){
document.querySelector(".display p").innerHTML = num;
if(temp2.length > 1){
document.querySelector(".display h6").innerHTML += num;
}
calculation.toggle = false;
}
});
}
//--------------------- This is for clicking clear btn ---------------------
const clearButton = document.querySelector(".clear"); // reset everything
clearButton.addEventListener("click", function(){
document.querySelector(".display p").innerHTML = "0";
document.querySelector(".display h6").innerHTML = "";
calculation.firstNum = 0;
calculation.operator = null;
calculation.secondNum = 0;
calculation.result = 0;
});
//--------------------- This is for clicking dot btn ---------------------
const dotButton = document.querySelector(".btn-dot");
dotButton.addEventListener("click", function(){
const temp = document.querySelector(".display p").innerHTML;
if(temp.includes(".")){
return;
}
else{
document.querySelector(".display p").innerHTML += ".";
document.querySelector(".display h6").innerHTML += ".";
}
});
//--------------------- This is for clicking fraction btn ---------------------
const fractionButton = document.querySelector(".btn-fraction");
fractionButton.addEventListener("click", function(){
const temp = document.querySelector(".display p").innerHTML;
const fractionNum = Number(temp) / 100;
document.querySelector(".display p").innerHTML = fractionNum;
document.querySelector(".display h6").innerHTML = fractionNum;
});
//--------------------- This is for clicking plus minus btn ---------------------
const plusMinusButton = document.querySelector(".plus-minus");
plusMinusButton.addEventListener("click", function(){
const temp = document.querySelector(".display p").innerHTML;
if(temp.includes("-")){
document.querySelector(".display p").innerHTML = temp.slice(1);
document.querySelector(".display h6").innerHTML = temp.slice(1);
}else {
document.querySelector(".display p").innerHTML = "-" + temp;
document.querySelector(".display h6").innerHTML = "-" + temp; //NEED TO FIX THIS
}
});
//--------------------- This is for math operator btn ---------------------
const operatorButton = document.querySelectorAll(".operator");
for(let i = 0; i < operatorButton.length; i++){
operatorButton[i].addEventListener("click", function(){
if(calculation.operator == null){ //for the first calculation
document.querySelector(".display h6").innerHTML += operatorButton[i].innerHTML;
calculation.operator = operatorButton[i].innerHTML;
calculation.firstNum = Number(document.querySelector(".display p").innerHTML);
document.querySelector(".display p").innerHTML = "0";
}
else if(calculation.operator != null){ //for the second calculation when pressed again
calculation.secondNum = Number(document.querySelector(".display p").innerHTML);
calculation.result = calculate(calculation.firstNum, calculation.secondNum, calculation.operator);
document.querySelector(".display p").innerHTML = calculation.result;
document.querySelector(".display h6").innerHTML =calculation.result + operatorButton[i].innerHTML;
calculation.firstNum = calculation.result;
calculation.operator = operatorButton[i].innerHTML; //update the operator
calculation.result = 0;//reset the result
calculation.toggle = true;//toggle this to be true when I press another number it will remove the current num on display and replace it with the new one
}
});
}
//--------------------- This is for clicking equal btn ---------------------
const equalButton = document.querySelector(".equal");
equalButton.addEventListener("click", function(){
if(calculation.operator == null){
return;
}
calculation.secondNum = Number(document.querySelector(".display p").innerHTML);
calculation.result = calculate(calculation.firstNum, calculation.secondNum, calculation.operator);
calculation.operator = null;
document.querySelector(".display p").innerHTML = calculation.result;
document.querySelector(".display h6").innerHTML = calculation.result;
});
function calculate(num1, num2, operator){
if(operator == "+"){
return num1 + num2;
}
else if(operator == "-"){
return num1 - num2;
}
else if(operator == "x"){
return num1 * num2;
}
else if(operator == "/"){
if(num2 == 0){
return "ERRRORRR";
}
return num1 / num2;
}
}