From 5e8ab7fee7e82789f2308fdedc4e4fcf15dcd310 Mon Sep 17 00:00:00 2001 From: Alexandra Domareski Date: Mon, 8 Dec 2025 15:29:37 +0100 Subject: [PATCH 1/2] make all the tests pass --- index.js | 51 ++++++++++++++++++++++++++++++++++++++++++++------- package.json | 5 ++++- 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 0f4b28b4..cff7d739 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 >= 0 && pos < this.items.length) { + return this.items[pos]; + } else { + throw new Error("OutOfBounds"); + } + } - max() {} + max() { + if(this.items.length === 0) { + throw new Error("EmptySortedList"); + } - min() {} + return Math.max(...this.items); + } - sum() {} + min() { + if(this.items.length === 0) { + throw new Error("EmptySortedList"); + } - avg() {} + return Math.min(...this.items); + } + + sum() { + return this.items.reduce((acc, val)=> { + return acc + val + }, 0) + } + + avg() { + if(this.items.length === 0) { + throw new Error("EmptySortedList"); + } + + return this.items.reduce(() => { + return this.sum() / this.items.length + }, 0) + } } 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" + } } From 646587977df37a705e0af0dd33bcf77e90d98412 Mon Sep 17 00:00:00 2001 From: Alexandra Domareski Date: Mon, 8 Dec 2025 15:36:16 +0100 Subject: [PATCH 2/2] fix syntax --- index.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index cff7d739..31f36522 100644 --- a/index.js +++ b/index.js @@ -28,7 +28,7 @@ class SortedList { min() { if(this.items.length === 0) { - throw new Error("EmptySortedList"); + throw new Error("Empty SortedList"); } return Math.min(...this.items); @@ -45,9 +45,7 @@ class SortedList { throw new Error("EmptySortedList"); } - return this.items.reduce(() => { - return this.sum() / this.items.length - }, 0) + return this.sum() / this.items.length; } }