Advertisement
ErolKZ

Untitled

Feb 15th, 2022
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. class List {
  2.  
  3. constructor() {
  4.  
  5. this.collection = [];
  6.  
  7. this.size = this.collection.length;
  8.  
  9. }
  10.  
  11. add(num) {
  12.  
  13. this.collection.push(num);
  14.  
  15. this.collection.sort((a, b) => a - b);
  16.  
  17. this.size = this.collection.length;
  18.  
  19. }
  20.  
  21. remove(index) {
  22.  
  23. if (this.collection.length - 1 >= index) {
  24.  
  25. this.collection.splice(index, 1);
  26.  
  27. this.size = this.collection.length;
  28.  
  29. } else {
  30.  
  31. throw Error('Index does not exist!');
  32.  
  33. }
  34.  
  35. }
  36.  
  37.  
  38. get(index) {
  39.  
  40. if (this.collection.length - 1 >= index) {
  41.  
  42. return this.collection[index];
  43.  
  44. } else {
  45.  
  46. throw Error('Index does not exist!');
  47.  
  48. }
  49.  
  50. }
  51.  
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement