Advertisement
Guest User

Untitled

a guest
Jun 5th, 2017
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.63 KB | None | 0 0
  1. class SortedList {
  2. constructor() {
  3. this.list = [];
  4. }
  5.  
  6. add(element) {
  7. this.list.push(element);
  8. this.sort();
  9. }
  10.  
  11. remove(index) {
  12. this.vrfyRange(index);
  13. this.list.splice(index, 1);
  14. }
  15.  
  16. get(index) {
  17. this.vrfyRange(index);
  18. return this.list[index];
  19. }
  20.  
  21. vrfyRange(index) {
  22. if (this.list.length == 0) throw new Error("Collection is empty.");
  23. if (index < 0 || index >= this.list.length) throw new Error("Index was outside the bounds of the collection.");
  24. }
  25.  
  26. sort() {
  27. this.list.sort((a, b) => a - b);
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement