From e8f5cedbe8b3e5c2d22e39c0fe97d68391748d84 Mon Sep 17 00:00:00 2001 From: SiyaM108 Date: Sat, 20 Dec 2025 11:17:45 +0000 Subject: [PATCH 1/3] run npm install mocha --- package.json | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 2940292..3c89e1f 100644 --- a/package.json +++ b/package.json @@ -12,5 +12,8 @@ "intro" ], "author": "supoort@rootlearn.com", - "license": "MIT" + "license": "MIT", + "dependencies": { + "mocha": "^11.7.5" + } } From d67ae78fa7f66615d43792fed45761bd0cda7025 Mon Sep 17 00:00:00 2001 From: SiyaM108 Date: Sat, 20 Dec 2025 11:34:45 +0000 Subject: [PATCH 2/3] complete constructor() --- index.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 0f4b28b..f7793d7 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,8 @@ class SortedList { - constructor() {} + constructor() { + this.items = []; + this.length = 0; + } add(item) {} From 8b4de7cf83aa9b0050b7402ab4c95bae80876a28 Mon Sep 17 00:00:00 2001 From: SiyaM108 Date: Sat, 20 Dec 2025 11:36:27 +0000 Subject: [PATCH 3/3] complete index.js --- index.js | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index f7793d7..e5cf2a1 100644 --- a/index.js +++ b/index.js @@ -4,17 +4,43 @@ class SortedList { 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("EmptyList"); + } + return this.items[this.length - 1]; + } - min() {} + min() { + if (this.length === 0) { + throw new Error("EmptyList"); + } + return this.items[0]; + } - sum() {} + sum() { + return this.items.reduce((acc, item) => acc + item, 0); + } - avg() {} + avg() { + if (this.length === 0) { + throw new Error("EmptyList"); + } + return this.sum() / this.length; + } } module.exports = SortedList;