-
Notifications
You must be signed in to change notification settings - Fork 0
/
temperatureTracker.js
56 lines (46 loc) · 1.03 KB
/
temperatureTracker.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
class TempTracker {
constructor() {
// for mean
this.mean = null;
this.totalSum = 0;
this.totalReadings = 0;
// for min and max
this.minTemp = null;
this.maxTemp = null;
// for mode
this.occurrences = new Array(111).fill(0);
this.maxOccurrences = 0;
this.mode = null;
}
insert(temp) {
// check the min and max temperatures
if (temp > this.maxTemp || this.maxTemp === null) {
this.maxTemp = temp;
}
if (temp < this.minTemp || this.minTemp === null) {
this.minTemp = temp;
}
// update the mean
this.totalReadings++;
this.totalSum += temp;
this.mean = this.totalSum / this.totalReadings;
// check the mode
this.occurrences[temp]++;
if (this.occurrences[temp] > this.maxOccurrences) {
this.maxOccurrences = this.occurrences[temp];
this.mode = temp;
}
}
getMean() {
return this.mean;
}
getMax() {
return this.max;
}
getMin() {
return this.min;
}
getMode() {
return this.currentModes[0];
}
}