diff --git a/index.js b/index.js index 0f4b28b4..8b539b87 100644 --- a/index.js +++ b/index.js @@ -1,17 +1,50 @@ class SortedList { - constructor() {} + constructor() { + this.items = [], + this.length = this.items.length + } - add(item) {} + add(item) { + this.items.unshift(item); + this.length = this.items.length; + return this.items.sort(function(a, b){return a-b}); + } - get(pos) {} + get(pos) { + if (pos < 0 || pos >= this.length){ + throw new Error("OutOfBounds"); + } + + return this.items[pos]; + } - max() {} + max() { + if (this.items.length === 0) { + throw new Error("Empty SortedList"); + } + return this.items.reduce((a, b) => Math.max(a, b), -Infinity); + } - min() {} + min() { + if (this.items.length === 0) { + throw new Error("Empty SortedList"); + } + return this.items.reduce((a, b) => Math.min(a, b)); + } - sum() {} + sum() { + if (this.items.length === 0) { + return 0; + } + return this.items.reduce((a, b) => a += b) + } - avg() {} + avg() { + if (this.items.length === 0) { + throw new Error("Empty SortedList"); + } + return this.items.reduce((a, b) => a += b)/this.items.length; + } } module.exports = SortedList; diff --git a/package.json b/package.json index 3a5127ae..932523a5 100644 --- a/package.json +++ b/package.json @@ -19,5 +19,8 @@ "intro" ], "author": "fer@ironhack.com", - "license": "MIT" + "license": "MIT", + "dependencies": { + "mocha": "^11.1.0" + } }