-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemptracker.js
95 lines (83 loc) · 3.01 KB
/
temptracker.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
/* Problem:
Write a class TempTracker with these methods:
insert() - records a new temperature
get_max() - returns the highest temp we've seen so far
get_min() - returns the lowest temp we've seen so far
get_mean() - returns the mean of all temps we've seen so far
get_mode() - returns a mode of all temps we've seen so far
Favour speeding up the getter methods get_max(), get_min(), get_mean(), and get_mode()
over speeding up the insert() method.
get_mean() should return a float, but the rest of the getter methods can return integers.
Temperatures will all be inserted as integers and we can assume they will be in the range 0
℃ to 150 ℃.
If there is more than one mode, return any of the modes.
*/
class TempTracker {
constructor(){
this.minTemperature = 0;
this.maxTemperature = 0;
this.noOfTemp = 0;
this.sumOfTemp = 0;
this.tempObject = {};
this.highestCount = 0;
}
/* All logics of inserting, min , max , mean, mode is implemented in insert method
and respective fnctions only return the values as we have to make it efficient.
*/
insert(temp) {
this.temperature = temp;
if(!this.minTemperature && !this.maxTemperature) {
this.maxTemperature = temp;
this.minTemperature = temp
} else {
if(temp > this.maxTemperature)
this.maxTemperature = temp;
if(temp < this.minTemperature)
this.minTemperature = temp;
}
//Mean calculation
this.noOfTemp++;
this.sumOfTemp += temp;
this.meanTemperature = this.sumOfTemp / this.noOfTemp;
//Mode calculation
this.tempObject[temp] = (this.tempObject[temp] || 0) + 1;
for(let item in this.tempObject) {
if(this.tempObject[item] > this.highestCount) {
this.modeTemperature = item;
this.highestCount = this.tempObject[item];
}
// console.log(item, this.tempObject[item], this.highestCount)
}
// console.log(this.tempObject);
// console.log(this.highestCount, this.modeTemperature);
}
get_max() {
return this.maxTemperature;
}
get_min() {
return this.minTemperature;
}
get_mean() {
return this.meanTemperature;
}
get_mode() {
return this.modeTemperature;
}
}
const tempTracker = new TempTracker();
// tempTracker.insert(1);
// tempTracker.insert(2);
// tempTracker.insert(4);
// tempTracker.insert(1);
tempTracker.insert(1)
tempTracker.insert(2)
tempTracker.insert(1)
tempTracker.insert(4);
tempTracker.insert(4);
tempTracker.insert(4);
tempTracker.insert(1);
tempTracker.insert(3)
console.log('min temperature -> ', tempTracker.get_min());
console.log('max temperature -> ', tempTracker.get_max());
console.log('mean temperature -> ', tempTracker.get_mean());
console.log('mode temperature -> ', tempTracker.get_mode());