-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcal.js
More file actions
193 lines (170 loc) · 4.61 KB
/
cal.js
File metadata and controls
193 lines (170 loc) · 4.61 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
let calOutput = document.getElementById("cal-output") ;
function addNumber(number){
calOutput.textContent += number ;
}
function addOperand(operand){
cal() ;
let output = calOutput.textContent ;
if (output[output.length-1]==" "){
calOutput.textContent = output.substring(0,output.length-3) + operand ;
}
else{
if (output!=""){
calOutput.textContent += operand ;
}
}
}
function addPercentage(){
let output = calOutput.textContent ;
let operand = [" + " ," - " , " * " ," / "] ;
let success = false ;
for (let i=0 ;i<output.length ;i++){
for (let i2=0 ;i2<operand.length ;i2++){
if(output.substring(i,i+3)==operand[i2]){
success = true ;
break ;
}
}
}
if (success){
console.log("%") ;
if (output[output.length-1] != " " & output[output.length-1] != "%"){
console.log("%") ;
calOutput.textContent += "%" ;
}
}
}
function positiveNegative(){
let output = calOutput.textContent ;
let operand = [" + " ," - " , " * " ," / "] ;
let calReq = false ;
for (let i=0 ;i<output.length ;i++){
for (let i2=0 ;i2<operand.length ;i2++){
if(output.substring(i,i+3)==operand[i2]){
calReq = true ;
break ;
}
}
}
let success ;
if (calReq){
success = cal();
if (success){
output = calOutput.textContent ;
output = Number(output) * -1 ;
calOutput.textContent = output ;
}
}
else{
output = Number(output) * -1 ;
calOutput.textContent = output ;
}
}
function removeText(){
let output = calOutput.textContent ;
if (output[output.length-1]==" "){
calOutput.textContent = output.substring(0,output.length-3) ;
}
else{
calOutput.textContent = output.substring(0,output.length-1) ;
}
}
function addFloat(){
output = calOutput.textContent ;
let operand = [" + " ," - " , " * " ," / "] ;
let data = [];
let index = 0 ;
let dataValid = false ;
while(index<operand.length){
data = output.split(operand[index]);
if (data.length==2){
dataValid = true ;
break ;
}
index ++;
}
if (dataValid){
num1 = data[0] ;
num2 = data[1] ;
let floatFound = false ;
for (let i=0 ;i<num2.length ;i++){
if(num2[i]=="."){
floatFound = true;
break ;
}
}
if (floatFound==false){
calOutput.textContent += "." ;
}
}
else{
num1 = output;
let floatFound = false ;
for (let i=0 ;i<num1.length ;i++){
if(num1[i]=="."){
floatFound = true ;
break ;
}
}
if (floatFound==false){
calOutput.textContent += "." ;
}
}
}
function cal(){
let processStr = calOutput.textContent ;
console.log(processStr) ;
let operand = [" + " ," - " , " * " ," / "] ;
let data = [];
let index = 0 ;
let dataValid = false ;
while(index<operand.length){
data = processStr.split(operand[index]);
if (data.length==2){
dataValid = true ;
break ;
}
index ++;
}
if (dataValid){
let num1 = data[0].replace(" ","") ;
let num2 = data[1].replace(" ","") ;
if (num1!="" & num2!=""){
num1 = Number(num1);
for (let i=0 ;i<num2.length ;i++){
if(num2[i] == "%"){
let percentage = Number(num2.substring(0,num2.length-1)) ;
if (index==0 | index==1){
num2 = num1/100*percentage ;
}
else{
num2 = percentage/100 ;
}
}
}
num2 = Number(num2);
let total;
if(index==0){
total = num1+num2 ;
}
else if(index==1){
total = num1-num2 ;
}
else if(index==2){
total = num1*num2 ;
}
else{
total = num1/num2 ;
}
console.log(num1,operand[index],num2,"=",total);
calOutput.textContent = total ;
return true ;
}
else{
return false}
}
}
function clearCal(){
calOutput.textContent = "" ;
console.log("clear") ;
}