Skip to content
Open
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
59 changes: 53 additions & 6 deletions starter-code/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,58 @@
class SortedList {
constructor() {
this.items = [];
this.length = 0;
}

add(item) {}
get(pos) {}
max() {}
min() {}
average() {}
sum() {}
add(item) {
this.items.push(item);
this.length = this.items.length;

this.items.sort(function (a, b) {
return a - b;
});
}

get(pos) {
if (pos > this.length) {
const error = new TypeError('OutOfBounds Error');
throw error;
}
return this.items[pos - 1];
}
max() {
if (this.length == 0) {
const error = new TypeError('EmptyList Error');
throw error;
}
return this.items[this.length - 1];

}
min() {
if (this.length == 0) {
const error = new TypeError('EmptyList Error');
throw error;
}
return this.items[0];
}
average() {
if (this.length == 0) {
const error = new TypeError('EmptyList Error');
throw error;
}
// const sum = this.items.reduce((a, b) => a + b, 0);
const avg = (this.sum() / this.items.length) || 0;
return avg;
}
sum() {
if (this.length == 0) {
const error = new TypeError('EmptyList Error');
throw error;
}
const sum = this.items.reduce((a, b) => a + b, 0);
return sum;
}
}


export default SortedList;
Loading