Skip to content
Open

done #2337

Show file tree
Hide file tree
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
42 changes: 35 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,45 @@
class SortedList {
constructor() {}
constructor(items, length) {
this.items = [];
this.length = this.items.length;
}

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 >= this.length) throw new Error("OutOfBounds");
return this.items[pos];
}

max() {}
max() {
if (!this.length) throw new Error("EmptySortedList");
let max = Math.max(...this.items);
return max;
}

min() {}
min() {
if (!this.length) throw new Error("Empty SortedList");
let min = Math.min(...this.items);
return min;
}

sum() {}
sum() {
if (!this.length) return 0;
let sum = this.items.reduce((acc, currentItem) => acc + currentItem);
return sum;
}

avg() {}
avg() {
if (!this.length) throw new Error("EmptySortedList");
let avg = this.sum() / this.length;
return avg;
}
}

module.exports = SortedList;


5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@
"intro"
],
"author": "[email protected]",
"license": "MIT"
"license": "MIT",
"dependencies": {
"mocha": "^11.1.0"
}
}