Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,60 @@
class SortedList {
class SortedList {
constructor() {}
constructor() {
this.items = [] // array
this.length = 0; // no. of elements in array.
}

add(item) {}
add(item) {
this.items.push(item)
this.items.sort((a,b)=>a-b);
this.length = this.items.length;
}

get(pos) {}
get(pos) {
if(pos<0 || pos>=this.length){
throw new Error("OutOfBounds")
}
return this.items[pos]
}

max() {}
max() {
if(this.length===0){
throw new Error("EmptySortedList")
}
return this.items[this.length-1]
}

min() {}
min() {
if(this.length===0){
throw new Error("EmptySortedList")
}
return this.items[0]
}

sum() {}
sum() {
return this.items.reduce((acc,curr)=>acc+curr,0)
}

avg() {}
}
avg() {
if(this.length===0){
throw new Error('EmptySortedList')
}
return this.sum()/this.length
}
}

module.exports = SortedList;

module.exports = SortedList;


module.exports = SortedList;