Advertisement
Guest User

Untitled

a guest
Aug 18th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.68 KB | None | 0 0
  1. interface Array<T> {
  2. insert(item: T, atEnd: boolean): void;
  3. remove(atEnd: boolean): void;
  4. atBeyond(index: number): T;
  5. }
  6.  
  7. Array.prototype.insert = function(item, atEnd) {
  8. if (atEnd) this.push(item);
  9. else this.unshift(item);
  10. };
  11.  
  12. Array.prototype.remove = function(atEnd) {
  13. if (atEnd) this.pop();
  14. else this.shift();
  15. };
  16.  
  17. function fixNegativeIndex(index: number, arrayLength: number): number {
  18. if (index < 0) {
  19. const countOfOver = Math.floor(-index / arrayLength);
  20. return index + arrayLength * (countOfOver + 1);
  21. }
  22. return index;
  23. }
  24.  
  25. Array.prototype.atBeyond = function(index) {
  26. const fixedIndex = fixNegativeIndex(index, this.length);
  27. return this[fixedIndex % this.length];
  28. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement