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

}
Comment on lines +2 to +6

add(item) {}
add(item) {
Comment on lines +3 to +8
let i = 0;
while (i < this.items.length && this.items[i] < item) i++;
this.items.splice(i, 0, item);
this.length = this.items.length;
}



get(pos) {}

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

min() {}
min() {
if (this.length === 0) throw new Error("EmptySortedList");
return Math.min(...this.items);
}
Comment on lines +22 to +30

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

avg() {}
avg() {
if (this.length === 0) throw new Error("EmptySortedList");

return this.items.reduce((acc, val) => acc + val, 0) / this.length;
}
}

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": "fer@ironhack.com",
"license": "MIT"
"license": "MIT",
"dependencies": {
"mocha": "^11.7.6"
}
}