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
55 changes: 48 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,58 @@
class SortedList {
constructor() {}
constructor() {
this.items = []; // Initialize items as an empty array
this.length = 0; // Initialize length to 0
}

add(item) {}
add(item) {
// Add the item to the array
this.items.push(item);
// Sort the array in ascending order
this.items.sort((a, b) => a - b);
// Update the length property
this.length = this.items.length;
}

get(pos) {}
get(pos) {
// Check if pos is out of bounds
if (pos < 0 || pos >= this.length) {
throw new Error('OutOfBounds');
}
// Return the item at the specified position
return this.items[pos];
}

max() {}
max() {
// Check if the items array is empty
if (this.length === 0) {
throw new Error('EmptySortedList');
}
// Return the maximum value
return this.items[this.length - 1];
}

min() {}
min() {
// Check if the items array is empty
if (this.length === 0) {
throw new Error('EmptySortedList');
}
// Return the minimum value
return this.items[0];
}

sum() {}
sum() {
// Return the sum of the items
return this.items.reduce((acc, curr) => acc + curr, 0);
}

avg() {}
avg() {
// Check if the items array is empty
if (this.length === 0) {
throw new Error('EmptySortedList');
}
// Return the average of the items
return this.sum() / this.length;
}
}

module.exports = SortedList;
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@
"intro"
],
"author": "supoort@rootlearn.com",
"license": "MIT"
"license": "MIT",
"dependencies": {
"mocha": "^10.7.3"
}
}