From 51aa82023d2858966c8582c18337b0eace54a643 Mon Sep 17 00:00:00 2001 From: Valentin Date: Mon, 8 Dec 2025 16:12:20 +0100 Subject: [PATCH] done --- index.js | 45 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 0f4b28b4..48f62b9f 100644 --- a/index.js +++ b/index.js @@ -1,15 +1,48 @@ class SortedList { - constructor() {} + constructor() { + this.length = 0; + this.items = []; + } - add(item) {} + add(item) { + this.length++; + this.items.push(item); + this.items.sort((a,b) => a - b); + } - get(pos) {} + get(pos) { + if(pos < 0 || pos >= this.length ){ + throw new Error("OutOfBounds"); + } + return this.items[pos]; + } - max() {} + max() { + if( this.items >= 0 ){ + throw new Error("EmptySortedList"); + } + return this.items [this.length - 1]; + } - min() {} + min() { + if( this.items >= 0 ){ + return new Exception("EmptySortedList"); + } + return this.items [0]; + } - sum() {} + sum() { + if (this.length === 0) { + return 0; + } + + let total = 0; + for (let i = 0; i < this.length; i++) { + total += this.items[i]; + } + + return total; + } avg() {} }