From 35f02156c3f837914e3175992aa308e822606038 Mon Sep 17 00:00:00 2001 From: YenaKC Date: Sun, 31 May 2026 10:59:43 +0200 Subject: [PATCH] done --- index.js | 51 ++++++++++++++++++++++++++++++++++++++++++++------- package.json | 5 ++++- 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 0f4b28b4..38b393d6 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) { + if (pos >= this.length) { + throw new Error("OutOfBounds"); + } - max() {} + return this.items[pos]; + } - min() {} + max() { + if (this.length === 0) { + throw new Error("EmptySortedList"); + } - sum() {} + return this.items[this.length - 1]; + } - avg() {} + min() { + if (this.length === 0) { + throw new Error("EmptySortedList"); + } + + return this.items[0]; + } + + sum() { + return this.items.reduce( + (total, number) => total + number, + 0 + ); + } + + + avg() { + if (this.length === 0) { + throw new Error("EmptySortedList"); + } + + return this.sum() / this.length; + } } module.exports = SortedList; diff --git a/package.json b/package.json index 3a5127ae..4f04f20d 100644 --- a/package.json +++ b/package.json @@ -19,5 +19,8 @@ "intro" ], "author": "fer@ironhack.com", - "license": "MIT" + "license": "MIT", + "devDependencies": { + "mocha": "^11.7.6" + } }