From 0fd184a492ba8c12e06d53d3ded49a02d247521e Mon Sep 17 00:00:00 2001 From: BrotherlyHamlet16 <80749000+Clever-Niwagaba@users.noreply.github.com> Date: Tue, 3 Dec 2024 11:19:52 +0300 Subject: [PATCH] completed --- index.js | 63 ++++++++++++++++++++++++++++++++++++++-------------- package.json | 35 ++++++++++++++++------------- 2 files changed, 65 insertions(+), 33 deletions(-) diff --git a/index.js b/index.js index 0f4b28b..285b02b 100644 --- a/index.js +++ b/index.js @@ -1,17 +1,46 @@ -class SortedList { - constructor() {} - - add(item) {} - - get(pos) {} - - max() {} - - min() {} - - sum() {} - - avg() {} -} - -module.exports = SortedList; +class SortedList { + constructor() { + this.items = []; + this.length = 0; + } + + add(item) { + this.items.push(item); + this.items.sort((a, b) => (a - b)); + this.length = this.items.length; + } + + get(pos) { + if (pos < 0 || pos >= this.length) + throw new Error('OutOfBounds'); + else + return this.items[pos]; + } + + max() { + if (this.length === 0) + throw new Error("EmptySortedList"); + else + return this.items[this.length - 1]; + } + + min() { + if (this.length === 0) + throw new Error("EmptySortedList"); + else + return this.items[0]; + } + + sum() { + return this.items.reduce((acc, curr) => acc + curr, 0); + } + + avg() { + if (this.length === 0) + throw new Error("EmptySortedList"); + else + return this.sum() / this.length; + } +} + +module.exports = SortedList; diff --git a/package.json b/package.json index 2940292..c333ce3 100644 --- a/package.json +++ b/package.json @@ -1,16 +1,19 @@ -{ - "name": "lab-intro-node", - "version": "2.0.0", - "description": "Intro Node Lab for Students", - "main": "index.js", - "scripts": { - "test": "mocha" - }, - "homepage": "https://github.com/root-labs/lab-intro-to-node#readme", - "keywords": [ - "node", - "intro" - ], - "author": "supoort@rootlearn.com", - "license": "MIT" -} +{ + "name": "lab-intro-node", + "version": "2.0.0", + "description": "Intro Node Lab for Students", + "main": "index.js", + "scripts": { + "test": "mocha" + }, + "homepage": "https://github.com/root-labs/lab-intro-to-node#readme", + "keywords": [ + "node", + "intro" + ], + "author": "supoort@rootlearn.com", + "license": "MIT", + "dependencies": { + "mocha": "^10.7.3" + } +}