diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..eecbcdc2 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,8 @@ +{ + "github.copilot.enable": { + "*": false, + "plaintext": false, + "markdown": false, + "scminput": false + } +} \ No newline at end of file diff --git a/index.js b/index.js index 0f4b28b4..5dc67046 100644 --- a/index.js +++ b/index.js @@ -1,17 +1,46 @@ class SortedList { - constructor() {} + // 1) should have items and length properties #add(x) + constructor() { + this.items = []; + this.length = 0; + } +// 2) should add a single value to SortedList - add(item) {} - - get(pos) {} - - max() {} + add(item) { + this.items.push(item); + this.items.sort((a,b) => a -b); + this.length = this.items.length; + } + get(pos) { + if (pos >= this.length) { + throw new Error("OutOfBounds"); + } + return this.items[pos]; +} - min() {} + max() { + if (this.length === 0) { + throw new Error("EmptySortedList"); + } + return this.items[this.length - 1]; +} + min() { + if (this.length === 0) { + throw new Error("EmptySortedList"); + } + return this.items[0]; +} - sum() {} + sum() { + return this.items.reduce((acc, val) => acc + val, 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 3a5127ae..f5d8555d 100644 --- a/package.json +++ b/package.json @@ -19,5 +19,8 @@ "intro" ], "author": "fer@ironhack.com", - "license": "MIT" + "license": "MIT", + "dependencies": { + "mocha": "^11.6.0" + } }