From f5157b3d1abaa33a90432de15acb88d5f0201ca9 Mon Sep 17 00:00:00 2001 From: Guia Date: Mon, 8 Dec 2025 16:29:49 +0100 Subject: [PATCH] solved lab --- index.js | 52 +++++++++++++++++++++++++++++++++++++++++++++------- package.json | 5 ++++- 2 files changed, 49 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 0f4b28b4..2966d13e 100644 --- a/index.js +++ b/index.js @@ -1,17 +1,55 @@ 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 - 1){ + return this.items[pos] + }else{ + throw new Error("OutOfBounds"); + } + } - max() {} + max() { + if (this.items.length > 0){ + this.items.sort((a, b) => b -a) + return this.items[0] + }else { + throw new Error("EmptySortedList"); + } + } - min() {} + min() { + if (this.items.length > 0){ + return Math.min(...this.items) + } else { + throw new Error("EmptySortedList"); + } + } - sum() {} + sum() { + if (this.items.length > 0){ + return this.items.reduce ((acc, el) => acc + el, 0) + } else { + return 0 + } + } - avg() {} + avg() { + if (this.items.length === 0) { + throw new Error('EmptySortedList'); + } + const sum = this.items.reduce((acc, el) => acc + el, 0); + return sum / this.items.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" + } }