-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
105 lines (91 loc) · 2.77 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
//factory function
function createCircle(radius) {
return {
radius,
draw: function() {
console.log('draw')
}
};
}
const circle = createCircle(1);
//constructor function
function Circle(radius) {
this.radius = radius;
this.draw = function() {
console.log('draw')
}
}
function StopWatch() {
//local stuff
let durationCount = [0, 0, 0];
let hasStarted = false;
let hasReset = true;
let startTime = [0, 0, 0]
let stopTime = [0, 0, 0]
function getTime() {
let d = new Date();
let timeStamp = [d.getHours(), d.getMinutes(), d.getSeconds()];
return timeStamp;
}
function setDuration() {
let start = startTime;
let stop = stopTime;
let hours = stopTime[0] - startTime[0];
let minutes = stopTime[1] - startTime[1];
let seconds = stopTime[2] - startTime[2];
if (seconds < 0) {
seconds += 60
minutes++
}
if (minutes < 0) {
minutes += 60;
hours++
}
if (hours < 0) {
hours += 60;
}
durationCount = [hours, minutes, seconds]
}
//Forward Facing Stuff
this.start = function() {
if (hasStarted === false && hasReset === true) {
startTime = getTime();
console.log(`Time Started @${startTime}`);
hasStarted = true;
hasReset = false;
} else {
throw new console.error('Time has already started!');
};
};
this.stop = function() {
if (hasStarted === true && hasReset === false) {
stopTime = getTime();
hasStarted = false;
console.log(`Time stopped @${stopTime}, time elapsed: ${stopTime[0] - startTime[0]} hours, ${stopTime[1] - startTime[1]} minutes, and ${stopTime[2] - startTime[2]} seconds`)
} else {
throw new console.error("Time hasn't started yet!")
};
};
this.duration = function() {
if (!hasReset && !hasStarted) {
setDuration()
console.log(`Time started @${startTime} stopped @${stopTime}, time elapsed: ${durationCount[0]} hours, ${durationCount[1]} minutes, and ${durationCount[2]} seconds`)
//console.log(durationCount)
} else {
console.log("The timer hasn't started yet, or is still running!")
}
}
this.reset = function() {
if (!hasReset) {
hasStarted = false;
hasReset = true;
startTime = [0, 0, 0];
stopTime = [0, 0, 0];
durationCount = [0, 0, 0];
console.log("Reset timer completed!")
} else {
console.log("The timer already reset!")
}
};
}
const sw = new StopWatch();