Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class SortedList {
- constructor() {
- this.list = [];
- }
- add(element) {
- this.list.push(element);
- this.sort();
- }
- remove(index) {
- this.vrfyRange(index);
- this.list.splice(index, 1);
- }
- get(index) {
- this.vrfyRange(index);
- return this.list[index];
- }
- vrfyRange(index) {
- if (this.list.length == 0) throw new Error("Collection is empty.");
- if (index < 0 || index >= this.list.length) throw new Error("Index was outside the bounds of the collection.");
- }
- sort() {
- this.list.sort((a, b) => a - b);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement