From 0bbe7425ed4d519e502bea685d1fc891fc3ceab0 Mon Sep 17 00:00:00 2001 From: Didac Odena Date: Wed, 3 Dec 2025 11:06:36 +0100 Subject: [PATCH] Sovled Lab. Itineration 6 test is incorrect --- index.js | 46 +++++++++++++++++++++++++++++++++++++++------- package.json | 5 ++++- prubea.js | 3 +++ 3 files changed, 46 insertions(+), 8 deletions(-) create mode 100644 prubea.js diff --git a/index.js b/index.js index 0f4b28b4..375a0c87 100644 --- a/index.js +++ b/index.js @@ -1,17 +1,49 @@ 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 < 0 || pos >= this.length) { + throw new Error("OutOfBounds"); + } + return this.items[pos]; + } - max() {} + max() { + if (this.length === 0) { + throw new Error("EmptySortedList"); + } + return this.items[this.length - 1]; + } - min() {} + min() { + if (this.length === 0) { + throw new Error("EmptySortedList"); + } + return this.items[0]; + } + + sum() { + if (this.length === 0) 0; + return this.items.reduce((acc, n) => acc + n, 0); + } + + avg() { + if (this.length === 0) { + throw new Error("EmptySortedList"); + } + return this.sum() / this.length; + } - sum() {} - 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" + } } diff --git a/prubea.js b/prubea.js new file mode 100644 index 00000000..0f6c3500 --- /dev/null +++ b/prubea.js @@ -0,0 +1,3 @@ + get(pos) { + this.items.indexOf(_, pos) + } \ No newline at end of file