diff --git a/index.js b/index.js index 0f4b28b..7726054 100644 --- a/index.js +++ b/index.js @@ -1,17 +1,46 @@ class SortedList { - constructor() {} + constructor() { + this.items = []; + 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 >= this.length || pos < 0) { + throw new Error('OutOfBounds'); + } + return this.items[pos]; + } - max() {} + max() { + if (this.length === 0) { + throw new Error('EmptySortedList'); + } + return Math.max(...this.items); + } - min() {} + min() { + if (this.length === 0) { + throw new Error('EmptySortedList'); + } + return Math.min(...this.items); + } - 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; diff --git a/package.json b/package.json index 2940292..d36e941 100644 --- a/package.json +++ b/package.json @@ -12,5 +12,8 @@ "intro" ], "author": "supoort@rootlearn.com", - "license": "MIT" + "license": "MIT", + "dependencies": { + "mocha": "^10.7.3" + } }