diff --git a/index.js b/index.js index 0f4b28b..af4a571 100644 --- a/index.js +++ b/index.js @@ -1,17 +1,48 @@ class SortedList { - constructor() {} + constructor() { + this.items = [] + this.length = 0 + } - add(item) {} + add(item) { + this.items.push(item) + this.length++ + this.items.sort((a, b) => a - b) + } - get(pos) {} + get(pos) { + if (pos < this.length){ + return this.items[pos] + } + throw new Error('OutOfBounds') + } - 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((sum, curr) => + curr + sum, 0 + ) + } - avg() {} + 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..2241c4d 100644 --- a/package.json +++ b/package.json @@ -12,5 +12,8 @@ "intro" ], "author": "supoort@rootlearn.com", - "license": "MIT" + "license": "MIT", + "dependencies": { + "mocha": "^11.1.0" + } }