forked from rexsteroxy/JavaScript_Tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
229 lines (202 loc) · 7.58 KB
/
script.js
File metadata and controls
229 lines (202 loc) · 7.58 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
(function(){
//using strict mode javascript
"use strict";
//defining my general counter
var counter = 2;
//defining my constant colors
const colors = [
{name: 'addition', value:'#e74c3c'},
{name: 'subtraction', value:'#2980b9'},
{name: 'division', value:'#2c1e7b'},
{name: 'multiplication', value:'#fc1e10'},
{name: 'modulo', value:'#e423a6'},
{name: 'background', value:'#f2e1a1'},
];
//function for increamenting counter
function incrementCounter(){
counter ++;
}
//function for decreamenting counter
function decrementCounter(){
counter --;
}
//function for zooming out
function zoomup(){
let answer = document.getElementById('resultDiv');
answer.style.fontSize = counter + 'em';
}
//function for zooming in
function zoomdown(){
let answer = document.getElementById('resultDiv');
answer.style.fontSize = counter + 'em';
}
//function for defaultDisplay
function defaultDisplay(text){
let answer = document.getElementById('resultDiv');
answer.innerHTML = text;
answer.style.fontSize = counter + 'em';
answer.style.color = colors[2].value ;
answer.style.backgroundColor = colors[5].value ;
let timeId = document.getElementById('time');
setInterval(()=>{
let time = new Date();
let hours = time.getHours();
let minutes = time.getMinutes();
let ampm = " AM"
if(hours >= 12){
hours -= 12;
ampm = " PM";
}else if(hours === 0){
hours = 12;
ampm = " AM";
}
if(minutes < 10){
minutes = '0' + minutes;
}
var sepClass = '';
if(time.getSeconds() % 2 === 1)
sepClass = 'trans';
var sep ='<span class="' + sepClass + '">:</span>';
let month = ['January','Febuary','March',
'April','May','June','july','August',
'September','October','November','December'];
timeId.innerHTML ='TIME: '+ hours + sep + time.getMinutes()
+':'+time.getSeconds() + ampm +' '+ 'DATE: '+ month[time.getMonth()]+':'+time.getDate();
},1000);
timeId.style.fontSize = counter + 'em';
timeId.style.color = colors[2].value ;
timeId.style.backgroundColor = colors[5].value ;
/* timeId.style.margin = "auto";
timeId.style.padding = "50px";
timeId.style.height = "30px";
timeId.style.width = "30px";
timeId.style.float = "right"; */
let num1 = document.getElementById('num1');
let num2 = document.getElementById('num2');
let signd = document.getElementById('operator');
let zoomup = document.getElementById('zoomup');
let zoomdown = document.getElementById('zoomdown');
let button = document.getElementById('button');
let reset= document.getElementById('reset');
let head = document.getElementById('head');
head.style.backgroundColor = colors[1].value ;
num1.style.margin = "30px"; num2.style.margin = "30px"; num1.style.padding = "10px"; num2.style.padding = "10px";
signd.style.padding = "10px"; zoomup.style.margin = "30px";zoomdown.style.margin = "30px";button.style.margin = "30px";
zoomdown.style.padding = "10px";zoomup.style.padding = "10px";button.style.padding = "10px"; reset.style.margin = "30px";
reset.style.padding = "10px";
}
//getting my button id for calculation
var calculateButton = document.getElementById('button');
//listening to load event once the page is refreshed
//this is a function for calculation display
function display(result){
let answer = document.getElementById('resultDiv');
answer.innerHTML = result;
let sign = document.getElementById('operator').value;
switch(sign){
case "+":
answer.style.color = colors[0].value ;
answer.style.backgroundColor = colors[5].value ;
break;
case "-":
answer.style.color = colors[1].value ;
break;
case "/":
answer.style.color = colors[2].value ;
break;
case "*":
answer.style.color = colors[3].value ;
break;
case "%":
answer.style.color = colors[4].value ;
break;
default:
answer.style.color = colors[2].value ;
}
}
//this is the function that will do the calculation
function calculate(){
let num1 = document.getElementById('num1');
let num2 = document.getElementById('num2');
let sign = document.getElementById('operator');
if(num1.value === ''){
alert('Please fill the first textbox');
num1.focus();
}else if( num2.value === ''){
alert('Please fill the second textbox');
num2.focus();
}
switch(sign.value){
case "+":
let sum = parseInt(num1.value,10) + parseInt(num2.value,10);
display(`Your result is: ${sum}`);
//alert(`Your result is: ${sum}`);
break;
case "-":
let sub = parseInt(num1.value,10) - parseInt(num2.value,10);
display(`Your result is: ${sub}`);
break;
case "*":
let mul = parseInt(num1.value,10) * parseInt(num2.value,10);
display(`Your result is: ${mul}`);
break;
case "/":
let div = parseInt(num1.value,10) / parseInt(num2.value,10);
display(`Your result is: ${div}`);
break;
case "%":
let modulo = parseInt(num1.value,10) % parseInt(num2.value,10);
display(`Your result is: ${modulo}`);
//display('That could be a modulo operator');
break;
default:
defaultDisplay('Fill the empty textbox');
}
}
//listening to the Dom for refresh
document.addEventListener('DOMContentLoaded', ()=>{
//function for default display once page loads
defaultDisplay('Your Result Will Display Here');
//disabled the button once inputs are empty
calculateButton.disabled = true;
//listening to input change
let num1 = document.getElementById('num1');
let num2 = document.getElementById('num2');
num1.addEventListener('change', ()=>{
if(num1.value === '' && num2.value === ''){
calculateButton.disabled = true;
}else{
calculateButton.disabled = false;
}
});
});
//listening to calculate button for its function
//https://www.linkedin.com/learning/programming-foundations-apis-and-web-services/what-you-should-know
calculateButton.addEventListener('click', ()=>{
calculate();
});
//getting and listening to reset button for its function
let myReset = document.getElementById('reset');
myReset.addEventListener('click', ()=>{
defaultDisplay('Your Result Will Display Here');
document.getElementById('num1').value='';
document.getElementById('num2').value='';
});
//getting and listening to zoomup button for its function
let myZoomButton = document.getElementById('zoomup');
myZoomButton.addEventListener('click', ()=>{
incrementCounter();
zoomup();
});
//getting and listening to zoomdown for its function
let myZoomButton2 = document.getElementById('zoomdown');
myZoomButton2.addEventListener('click', ()=>{
if(counter <= 2){
counter = 2;
}
else{
decrementCounter();
zoomdown();
}
});
})();