viligen

sortedListClasses

May 28th, 2022
942
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function createSortedList() {
  2.     class NewList {
  3.         constructor() {
  4.             this.numbers = [];
  5.             this.size = this.numbers.length;
  6.         }
  7.         add(element) {
  8.             this.numbers.push(element);
  9.             this.numbers.sort((a, b) => a - b);
  10.             this.size = this.numbers.length;
  11.         }
  12.         remove(index) {
  13.             if (index >= 0 && index < this.numbers.length) {
  14.                 this.numbers.splice(index, 1);
  15.                 this.numbers.sort((a, b) => a - b);
  16.                 this.size = this.numbers.length;
  17.             }
  18.         }
  19.         get(index) {
  20.             if (index >= 0 && index < this.numbers.length) {
  21.                 return this.numbers[index];
  22.             }
  23.         }
  24.     }
  25.    
  26.     return new NewList();
  27. }
  28.  
  29. let list = createSortedList();
  30. list.add(7);
  31. list.add(5);
  32. list.add(6);
  33. console.log(list.size);
  34. console.log(list.get(1));
  35. list.remove(1);
  36. console.log(list.get(1));
  37. console.log(list.size);
  38.  
Advertisement
Add Comment
Please, Sign In to add comment