-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdigital_analog_clock.js
61 lines (53 loc) · 1.91 KB
/
digital_analog_clock.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
class Clock {
constructor() { }
currentTime() {
var today = new Date();
this.hour = today.getHours();
this.minute = today.getMinutes();
this.second = today.getSeconds();
this.twelveHours = this.hour % 12;
}
init() {
this.bindEvents();
}
bindEvents() {
this.displayTimeTriggerElement.addEventListener("click", this.displayTime.bind(this));
}
}
class AnalogClock extends Clock {
constructor(displayTimeTriggerElement) {
super();
this.displayTimeTriggerElement = displayTimeTriggerElement;
}
displayTime() {
this.currentTime();
this.hourHand = this.twelveHours * 30;
this.minuteHand = ((Math.trunc(this.minute / 5) * 30) + ((this.minute % 5) * 6));
this.secondsHand = ((Math.trunc(this.second / 5) * 30) + ((this.second % 5) * 6));
alert("Analog Time Format :- " + "\n" +
"Hour Hand at " + this.hourHand + "deg" + "\n" +
"Minute Hand at " + this.minuteHand + "deg" + "\n" +
"Second Hand at " + this.secondsHand + "deg");
}
}
class DigitalClock extends Clock {
constructor(displayTimeTriggerElement) {
super();
this.displayTimeTriggerElement = displayTimeTriggerElement;
}
displayTime() {
this.currentTime();
this.ampm = this.hour >= 12 ? 'PM' : 'AM';
this.digitalHours = this.twelveHours;
this.digitalHours = this.twelveHours ? this.twelveHours : '12';
this.minute = this.minute < 10 ? '0' + this.minute : this.minute;
this.second = this.second < 10 ? '0' + this.second : this.second;
alert("Digital Time Format :- " + "\n" + this.digitalHours + ":" + this.minute + ":" + this.second + " " + this.ampm);
}
}
var displayTimeTriggerElement = document.getElementById("clock-analog")
var analogVar = new AnalogClock(displayTimeTriggerElement);
analogVar.init();
displayTimeTriggerElement = document.getElementById("clock-digital")
var digitalVar = new DigitalClock(displayTimeTriggerElement);
digitalVar.init();