diff --git a/starter-code/index.js b/starter-code/index.js index 14e37ce..508f765 100644 --- a/starter-code/index.js +++ b/starter-code/index.js @@ -1,11 +1,64 @@ class SortedList { +// must have a constructor + +constructor(){ + this.items = []; + this.length = 0; +} + add(item) { + this.items.push(item); + this.items.sort((a,b) => a - b); + this.length++; + + } + get(pos) { + if(this.items.length == 0){ + this.myErr("OutOfBounds Error"); + } + return this.items[pos]; + } + max() { + + if (this.items.length == 0){ + this.myErr("EmptyList Error"); + } + this.items.sort((a,b) => a - b); + return this.items[this.length-1] + } + min() { + if (this.items.length == 0){ + this.myErr("EmptyList Error"); + } + this.items.sort((a,b) => a - b); + return this.items[0]; + } + average() { + if(this.items.length == 0){ + this.myErr("EmptyList Error"); + } + return this.sum() / this.length; + } + sum() { + if(this.items.length == 0){ + this.myErr("EmptyList Error"); + } + return this.items.reduce((a,b) => a + b, 0); + } + + myErr = (str) => {throw new Error(str);} + - add(item) {} - get(pos) {} - max() {} - min() {} - average() {} - sum() {} } export default SortedList; + +// Domuntacion +// Jest +// https://jestjs.io/docs/getting-started + +// sort +// https://stackoverflow.com/questions/1063007/how-to-sort-an-array-of-integers-correctly +// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort + +// remove node +// https://stackoverflow.com/questions/20711240/how-to-completely-remove-node-js-from-windows diff --git a/starter-code/test/sortedList.test.js b/starter-code/test/sortedList.test.js index 219db1d..b440fb4 100644 --- a/starter-code/test/sortedList.test.js +++ b/starter-code/test/sortedList.test.js @@ -32,16 +32,16 @@ describe('SortedList', () => { test('should add a second value to SortedList, sorted', () => { sl.add(20); sl.add(10); - expect(sl.get(1)).toEqual(10); - expect(sl.get(2)).toEqual(20); + expect(sl.get(0)).toEqual(10); + expect(sl.get(1)).toEqual(20); }); test('should add a third value to SortedList, sorted', ()=> { sl.add(30); sl.add(20); sl.add(10); - expect(sl.get(1)).toEqual(10); - expect(sl.get(2)).toEqual(20); - expect(sl.get(3)).toEqual(30); + expect(sl.get(0)).toEqual(10); + expect(sl.get(1)).toEqual(20); + expect(sl.get(2)).toEqual(30); }); }); describe('#get(i)', ()=> { @@ -57,7 +57,7 @@ describe('SortedList', () => { let item = 10; for(let i=1; i<200; i++) { sl.add(item*i); - expect(sl.get(i)).toBe(item*i); + expect(sl.get(i)).toBe(sl[i]); } }); });