Advertisement
kstoyanov

09. Sorted List

Sep 22nd, 2020
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class List {
  2.   constructor() {
  3.     this.arr = [];
  4.     this.size = 0;
  5.   }
  6.  
  7.   add(element) {
  8.     this.arr.push(element);
  9.     this.size += 1;
  10.     return this.arr.sort((a, b) => a - b);
  11.   }
  12.  
  13.   remove(index) {
  14.     if (this.arr.length > index && index >= 0) {
  15.       this.arr.splice(index, 1);
  16.       this.size--;
  17.     } else {
  18.       throw new Error();
  19.     }
  20.     return this.arr.sort((a, b) => a - b);
  21.   }
  22.  
  23.   get(index) {
  24.     if (this.arr.length > index && index >= 0) {
  25.       return this.arr[index];
  26.     }
  27.     throw new Error();
  28.   }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement