Advertisement
bebo231312312321

Untitled

Jun 8th, 2023
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class List {
  2.     constructor(list = []) {
  3.         this.list = list.sort((a, b) => a - b)
  4.         this.size = 0
  5.     }
  6.     add(num) {
  7.  
  8.         this.list.push(num)
  9.         this.size++
  10.         this.list.sort((a, b) => a - b)
  11.     }
  12.  
  13.     remove(num) {
  14.         if (num < 0 || num >= this.size) throw new Error('Invalid Index Position')
  15.         this.list.splice(num, 1)
  16.         this.size--
  17.     }
  18.     get(index) {
  19.         if (index < 0 || index >= this.size) throw new Error('Invalid Index Position');
  20.         return this.list[index]
  21.     }
  22. }
  23. let list = new List(); list.add(5);
  24. list.add(6);
  25. list.add(7); console.log(list.get(1));
  26. list.remove(1); console.log(list.get(1));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement