From 2409dd1ae7d5ae2d68a4881ba3d75c28dc914112 Mon Sep 17 00:00:00 2001 From: AdityaKoche01 Date: Sun, 11 Feb 2024 13:31:03 +0530 Subject: [PATCH] done --- index.js | 57 +++++++++++++++++++++++++++++++++++++++++++++------- package.json | 5 ++++- 2 files changed, 54 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 0f4b28b..39db989 100644 --- a/index.js +++ b/index.js @@ -1,17 +1,60 @@ class SortedList { - constructor() {} + constructor() { + this.length = 0; + this.items = []; + } - add(item) {} + add(item) { + this.items.push(item); + this.items.sort((a,b) => a - b); + this.length++; + } - get(pos) {} + get(pos) { + if (pos <0 || pos >= this.length){ + throw new Error("OutOfBounds"); + } + return this.items[pos]; - max() {} + } - min() {} + max() { + if(this.length == 0){ + throw new Error('EmptySortedList'); - sum() {} - avg() {} + } + return this.items[this.length - 1]; + } + + min() { + if(this.length == 0){ + throw new Error('EmptySortedList'); + + + } + return this.items[0]; + } + + sum() { + if(this.length == 0){ + return 0; + + + } + return this.items.reduce((a,b) => a + b , 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 2940292..c952e46 100644 --- a/package.json +++ b/package.json @@ -12,5 +12,8 @@ "intro" ], "author": "supoort@rootlearn.com", - "license": "MIT" + "license": "MIT", + "dependencies": { + "mocha": "^10.3.0" + } }