From 3baa907f739f74839a8833c630346dc65d33a5f6 Mon Sep 17 00:00:00 2001 From: Sahil Date: Tue, 11 Jun 2024 13:33:53 +0530 Subject: [PATCH] done --- index.js | 48 +++++++++++++++++++++++++++++++++++++++--------- package.json | 5 ++++- 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index 0f4b28b..41d74b8 100644 --- a/index.js +++ b/index.js @@ -1,17 +1,47 @@ class SortedList { - constructor() {} + constructor() { + this.items = [] // array + this.length = 0; // no. of elements in array. + } - 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("EmptySortedList") + } + return this.items[this.length-1] + } - min() {} + min() { + if(this.length===0){ + throw new Error("EmptySortedList") + } + return this.items[0] + } - sum() {} + sum() { + return this.items.reduce((acc,curr)=>acc+curr,0) + } - avg() {} -} + avg() { + if(this.length===0){ + throw new Error('EmptySortedList') + } + return this.sum()/this.length + } + } -module.exports = SortedList; + +module.exports = SortedList; \ No newline at end of file diff --git a/package.json b/package.json index 2940292..8f04c69 100644 --- a/package.json +++ b/package.json @@ -12,5 +12,8 @@ "intro" ], "author": "supoort@rootlearn.com", - "license": "MIT" + "license": "MIT", + "dependencies": { + "mocha": "^10.4.0" + } }