-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathindex.js
More file actions
48 lines (41 loc) · 790 Bytes
/
index.js
File metadata and controls
48 lines (41 loc) · 790 Bytes
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
class SortedList {
constructor() {
this.items = []
this.length = 0
}
add(item) {
this.items.push(item)
this.length++
this.items.sort((a, b) => a - b)
}
get(pos) {
if (pos < this.length){
return this.items[pos]
}
throw new Error('OutOfBounds')
}
max() {
if (this.length === 0){
throw new Error('EmptySortedList')
}
return this.items[this.length - 1]
}
min() {
if (this.length === 0){
throw new Error('EmptySortedList')
}
return this.items[0]
}
sum() {
return this.items.reduce((sum, curr) =>
curr + sum, 0
)
}
avg() {
if (this.length === 0){
throw new Error('EmptySortedList')
}
return this.sum() / this.length
}
}
module.exports = SortedList;