From 132c279bfd737931e975a5e0864ade6e6dfc8f82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Bail=C3=B3n?= Date: Mon, 9 Mar 2026 14:38:44 +0100 Subject: [PATCH] Solved Lab --- index.js | 51 ++++++++++++++++++++++++++++++++++++++++++++------- package.json | 5 ++++- 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 0f4b28b4..2db599d9 100644 --- a/index.js +++ b/index.js @@ -1,17 +1,54 @@ 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) { - max() {} + if (pos < 0 || pos >= this.length) { + throw new Error("OutOfBounds"); + } - min() {} + return this.items[pos]; + } - sum() {} + max() { + if (this.items.length === 0) { + throw new Error("EmptySortedList"); + } + return this.items[this.length - 1]; + } - avg() {} + min() { + if (this.items.length === 0) { + throw new Error("Empty SortedList"); + } + return this.items[0]; + } + + sum() { + let total = 0; + + for (let i = 0; i < this.items.length; i++) { + total += this.items[i]; + } + + return total; + } + + avg() { + if (this.items.length === 0) { + throw new Error("Empty SortedList"); + } + return this.sum() / this.length; + } } 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" + } }