From fdd931c60a325d9db6a6637b043af000b421cf6d Mon Sep 17 00:00:00 2001 From: Sreenima05-09-03 <131434989+Sreenima05-09-03@users.noreply.github.com> Date: Thu, 26 Sep 2024 18:46:07 +0530 Subject: [PATCH] Update index.js --- index.js | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/index.js b/index.js index 0f4b28b..0004e3d 100644 --- a/index.js +++ b/index.js @@ -1,17 +1,60 @@ class SortedList { + 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; + module.exports = SortedList;