Advertisement
viligen

sortedListObject

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