Advertisement
didkoslawow

Untitled

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