diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 00000000..c2261ba5 Binary files /dev/null and b/.DS_Store differ diff --git a/index.js b/index.js index 0f4b28b4..70f29616 100644 --- a/index.js +++ b/index.js @@ -1,17 +1,45 @@ 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 < 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() {} - avg() {} + avg() { + if (this.length === 0) { + throw new Error ("EmptySortedList"); + } + const total = this.sum(); + return total / this.length; + } } module.exports = SortedList; diff --git a/package.json b/package.json index 3a5127ae..2e1f0a9d 100644 --- a/package.json +++ b/package.json @@ -19,5 +19,9 @@ "intro" ], "author": "fer@ironhack.com", - "license": "MIT" + "license": "MIT", + "devDependencies": { + "chai": "^6.2.1", + "mocha": "^11.7.5" + } }