Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 59 additions & 6 deletions starter-code/index.js
Original file line number Diff line number Diff line change
@@ -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
12 changes: 6 additions & 6 deletions starter-code/test/sortedList.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)', ()=> {
Expand All @@ -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]);
}
});
});
Expand Down