From ab598af9c13d7c655c83c4de1381ba489efa5087 Mon Sep 17 00:00:00 2001 From: superquique Date: Mon, 9 Mar 2026 10:41:44 -0600 Subject: [PATCH 1/2] Implement SortedList class --- index.js | 59 +++++++++++++++++++++++++++++++++++++++++++++------- package.json | 5 ++++- 2 files changed, 56 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 0f4b28b4..6b73d57e 100644 --- a/index.js +++ b/index.js @@ -1,17 +1,62 @@ class SortedList { - constructor() {} + constructor() { + this.items = []; + this.length = 0; + } - 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.items.length) { + throw new Error("OutOfbounds"); + } - max() {} + return this.items[pos]; + } - min() {} + max() { + if (this.items.length === 0) { + throw new Error("EmptySortedList"); + } - sum() {} + return Math.max(...this.items); + } - avg() {} + min() { + if (this.items.length === 0) { + throw new Error ("EmptySortedList"); + } + + return Math.min(...this.items); + } + + sum() { + if (this.items.length === 0) { + return 0; + } + + const total = this.items.reduce((acc, curr) => { + return acc + curr; + }, 0); + + return total; + } + + avg() { + if (this.items.length === 0) { + throw new Error("EmptySortedList"); + } + + const avg = this.items.reduce((acc, curr, index, arr) => { + return acc + (curr/arr.length); + }, 0); + + return avg; + } } module.exports = SortedList; diff --git a/package.json b/package.json index 3a5127ae..f2ff4434 100644 --- a/package.json +++ b/package.json @@ -19,5 +19,8 @@ "intro" ], "author": "fer@ironhack.com", - "license": "MIT" + "license": "MIT", + "dependencies": { + "mocha": "^11.7.5" + } } From 6bd0632b43493871ceeb829fd1dabf5b76d40468 Mon Sep 17 00:00:00 2001 From: superquique Date: Mon, 9 Mar 2026 10:49:08 -0600 Subject: [PATCH 2/2] fix: out of bounds exception --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 6b73d57e..b5721b53 100644 --- a/index.js +++ b/index.js @@ -11,8 +11,8 @@ class SortedList { } get(pos) { - if (pos > this.items.length) { - throw new Error("OutOfbounds"); + if (pos < 0 || pos > this.items.length) { + throw new Error("OutOfBounds"); } return this.items[pos];