From 9cf6b732d8bad6fa928919114e650cb3856ff41d Mon Sep 17 00:00:00 2001 From: Roberto Garcia <41040915+naranjito72@users.noreply.github.com> Date: Wed, 15 Sep 2021 08:33:55 +0200 Subject: [PATCH 1/5] half way through First exercise of jest testing --- starter-code/index.js | 62 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 59 insertions(+), 3 deletions(-) diff --git a/starter-code/index.js b/starter-code/index.js index 14e37ce..f77214c 100644 --- a/starter-code/index.js +++ b/starter-code/index.js @@ -1,11 +1,67 @@ class SortedList { +// must have a constructor - add(item) {} - get(pos) {} - max() {} +constructor(){ + this.items = []; + this.length = 0; + this.myHelper = false; +} + add(item) { + this.items.push(item); + this.mySort(this.items); + this.length++; + + } + get(pos) { + this.helpMe(); + if(this.myHelper == false){ + this.myErr("OutOfBounds Error"); + } else { + let arrPos; + arrPos = this.items[pos]; + return arrPos; + } + } + max() { + this.helpMe(); + let maxNum = 0; + if(this.myHelper == false){ + this.myErr("EmptyList Error"); + } else if(this.items.length == 1){ + this.items = this.mySort(this.items); + maxNum = this.items[0]; + console.log(maxNum) + } else { + this.items = this.mySort(this.items); + maxNum = this.items[-1]; + } + return maxNum; + } min() {} average() {} sum() {} + // sort + mySort = (arr) => arr.sort((a, b) => a - b); + // helper + helpMe = () => (this.items.length == 0) ? this.myHelper : this.myHelper = true; + // error + myErr = (str) => { + let err = new Error(str); + throw err; + } + } 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 + From 5dbfc35cfd7217fa314d6447c4a27abe7dcf86c5 Mon Sep 17 00:00:00 2001 From: Roberto Garcia <41040915+naranjito72@users.noreply.github.com> Date: Wed, 15 Sep 2021 08:35:53 +0200 Subject: [PATCH 2/5] first changes Position items[i] Positions sl.get(0) --- starter-code/test/sortedList.test.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) 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]); } }); }); From 5932a13dad62f2b0fad69f451a5d103ee53fdc69 Mon Sep 17 00:00:00 2001 From: Roberto Garcia <41040915+naranjito72@users.noreply.github.com> Date: Wed, 15 Sep 2021 10:49:10 +0200 Subject: [PATCH 3/5] Finished unpolished --- starter-code/index.js | 54 ++++++++++++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 16 deletions(-) diff --git a/starter-code/index.js b/starter-code/index.js index f77214c..1bb91c7 100644 --- a/starter-code/index.js +++ b/starter-code/index.js @@ -8,42 +8,64 @@ constructor(){ } add(item) { this.items.push(item); - this.mySort(this.items); + // this.mySort(this.items); + this.items.sort((a,b) => a - b); this.length++; } get(pos) { - this.helpMe(); - if(this.myHelper == false){ + // this.helpMe(); + if(this.items.length == 0){ this.myErr("OutOfBounds Error"); - } else { + } /* else { let arrPos; arrPos = this.items[pos]; return arrPos; - } + } */ + return this.items[pos]; } max() { - this.helpMe(); - let maxNum = 0; + /* this.helpMe(); */ + /*let maxNum = 0; + let large = this.items.length; if(this.myHelper == false){ this.myErr("EmptyList Error"); - } else if(this.items.length == 1){ + } else if(this.items.length == 1){ this.items = this.mySort(this.items); maxNum = this.items[0]; - console.log(maxNum) } else { this.items = this.mySort(this.items); - maxNum = this.items[-1]; + maxNum = this.items.length -1; + } */ + if (this.items.length == 0){ + this.myErr("EmptyList Error"); } - return maxNum; + 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.items.reduce((a,b) => a + b, 0) / this.length; + } + sum() { + if(this.items.length == 0){ + this.myErr("EmptyList Error"); + } + return this.items.reduce((a,b) => a + b, 0); } - min() {} - average() {} - sum() {} // sort - mySort = (arr) => arr.sort((a, b) => a - b); + // mySort = (arr) => arr.sort((a, b) => a - b); // helper - helpMe = () => (this.items.length == 0) ? this.myHelper : this.myHelper = true; + // helpMe = () => (this.items.length == 0) ? this.myHelper : this.myHelper = true; // error myErr = (str) => { let err = new Error(str); From 2ea0c2d0e8372e3afadb656150a87b5ab13b3fe7 Mon Sep 17 00:00:00 2001 From: Roberto Garcia <41040915+naranjito72@users.noreply.github.com> Date: Wed, 15 Sep 2021 10:55:58 +0200 Subject: [PATCH 4/5] Finished refac Thanks to my mate I refac the code --- starter-code/index.js | 28 +++------------------------- 1 file changed, 3 insertions(+), 25 deletions(-) diff --git a/starter-code/index.js b/starter-code/index.js index 1bb91c7..1d3cbbd 100644 --- a/starter-code/index.js +++ b/starter-code/index.js @@ -4,39 +4,21 @@ class SortedList { constructor(){ this.items = []; this.length = 0; - this.myHelper = false; } add(item) { this.items.push(item); - // this.mySort(this.items); this.items.sort((a,b) => a - b); this.length++; } get(pos) { - // this.helpMe(); if(this.items.length == 0){ this.myErr("OutOfBounds Error"); - } /* else { - let arrPos; - arrPos = this.items[pos]; - return arrPos; - } */ + } return this.items[pos]; } max() { - /* this.helpMe(); */ - /*let maxNum = 0; - let large = this.items.length; - if(this.myHelper == false){ - this.myErr("EmptyList Error"); - } else if(this.items.length == 1){ - this.items = this.mySort(this.items); - maxNum = this.items[0]; - } else { - this.items = this.mySort(this.items); - maxNum = this.items.length -1; - } */ + if (this.items.length == 0){ this.myErr("EmptyList Error"); } @@ -62,11 +44,7 @@ constructor(){ } return this.items.reduce((a,b) => a + b, 0); } - // sort - // mySort = (arr) => arr.sort((a, b) => a - b); - // helper - // helpMe = () => (this.items.length == 0) ? this.myHelper : this.myHelper = true; - // error + myErr = (str) => { let err = new Error(str); throw err; From 8c2d7adf27d989bb7f0ddeeb1d685550e3dce63e Mon Sep 17 00:00:00 2001 From: Roberto Garcia <41040915+naranjito72@users.noreply.github.com> Date: Wed, 15 Sep 2021 19:40:53 +0200 Subject: [PATCH 5/5] Refac code Using sum instead of reduce --- starter-code/index.js | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/starter-code/index.js b/starter-code/index.js index 1d3cbbd..508f765 100644 --- a/starter-code/index.js +++ b/starter-code/index.js @@ -18,25 +18,25 @@ constructor(){ 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] + 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]; + return this.items[0]; } average() { if(this.items.length == 0){ this.myErr("EmptyList Error"); } - return this.items.reduce((a,b) => a + b, 0) / this.length; + return this.sum() / this.length; } sum() { if(this.items.length == 0){ @@ -44,11 +44,9 @@ constructor(){ } return this.items.reduce((a,b) => a + b, 0); } - - myErr = (str) => { - let err = new Error(str); - throw err; - } + + myErr = (str) => {throw new Error(str);} + } @@ -64,4 +62,3 @@ export default SortedList; // remove node // https://stackoverflow.com/questions/20711240/how-to-completely-remove-node-js-from-windows -