From 44b2d55ea0daa862fed0b5a96efe852c07a4fcd0 Mon Sep 17 00:00:00 2001 From: Florian Wunsch Date: Tue, 22 Apr 2025 08:40:33 +0200 Subject: [PATCH 1/2] Done --- index.js | 45 ++++++++++++++++++++++++++++++++++++++------- package.json | 5 ++++- 2 files changed, 42 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 0f4b28b4..e27bc364 100644 --- a/index.js +++ b/index.js @@ -1,17 +1,48 @@ 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("Empty SortedList") + } + return this.items[0]; + } - sum() {} + sum() { + if (this.length === 0){ + return 0; + } + return this.items.reduce((add, item) => add + item, 0) + } - avg() {} + avg() { + if (this.length === 0) { + return 0; + } + return this.sum() / this.length; + } } module.exports = SortedList; diff --git a/package.json b/package.json index 3a5127ae..932523a5 100644 --- a/package.json +++ b/package.json @@ -19,5 +19,8 @@ "intro" ], "author": "fer@ironhack.com", - "license": "MIT" + "license": "MIT", + "dependencies": { + "mocha": "^11.1.0" + } } From 0d2e0783ebc1e5ce7305511a7fd54e335d35b9e2 Mon Sep 17 00:00:00 2001 From: Florian Wunsch Date: Tue, 22 Apr 2025 08:41:11 +0200 Subject: [PATCH 2/2] done2 --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index e27bc364..4dd8f11c 100644 --- a/index.js +++ b/index.js @@ -6,7 +6,7 @@ class SortedList { add(item) { this.items.push(item); - this.items.sort((a, b) => a - b); + this.items.sort((a, z) => a - z); this.length = this.items.length; }