text: class Node {
    constructor(val) {
      this.val = val;
      this.next = null;
      this.prev = null;
    }
  }
  
  class DoublyLinkedList {
    constructor() {
      this.head = null;
      this.tail = null;
      this.length = 0;
    }
    push(val) {
      let newNode = new Node(val);
      if (!this.head) {
        this.head = newNode;
        this.tail = newNode;
      } else {
        this.tail.next = newNode;
        newNode.prev = this.tail;
        this.tail = newNode;
      }
      this.length++;
      return this;
    }
    pop() {
      if (!this.head) return undefined;
      let poppedNode = this.tail;
      if (this.length === 1) {
        this.head = null;
        this.tail = null;
      } else {
        this.tail = poppedNode.prev;
        this.tail.next = null;
        poppedNode.prev = null;
      }
      this.length--;
      return poppedNode;
    }
    shift() {
      if (this.length === 0) return undefined;
      let oldHead = this.head;
      if (this.length === 1) {
        this.head = null;
        this.tail = null;
      } else {
        this.head = oldHead.next;
        this.head.prev = null;
        oldHead.next = null;
      }
      this.length--;
      return oldHead;
    }
    unshift(val) {
      let newNode = new Node(val);
      if (this.length === 0) {
        this.head = val;
        this.tail = val;
      } else {
        this.head.prev = val;
        newNode.next = this.head;
        this.head = newNode;
      }
      this.length++;
      return this;
    }
    get(index) {
      if (index < 0 || index >= this.length) return null;
      let count = 0;
      if (index < this.length / 2) {
        console.log('START');
        while (count != index) {
          let current = this.head.next;
          count += 1;
          return current;
        }
      } else {
        console.log('END');
        while (count != index) {
          let current = this.tail.prev;
          count += 1;
          return current;
        }
      }
    }
    set(index, val) {
      let foundNode = this.get(index);
      if (foundNode != null) {
        foundNode.val = val;
        return true;
      }
      return false;
    }
    insert(index, val) {
      if (index < 0 || index > this.length) return false;
      if (index === 0) {
        this.unshift(val);
      }
      if (index === this.length) {
        this.push(val);
      } else {
        newNode = new Node(val);
        let prevNode = this.get(index - 1);
        let followingNode = prevNode.next;
        prevNode.next = newNode;
        followingNode.prev = newNode;
        newNode.prev = prevNode;
        newNode.next = followingNode;
      }
      this.length++;
      return true;
    }
    remove(index) {
      if (index < 0 || index >= this.length) return undefined;
      if (index === 0) return this.shift();
      if (index === this.length - 1) return this.pop();
      let removedNode = this.get(index);
      removedNode.prev.next = removedNode.next;
      removedNode.next.prev = removedNode.prev;
      removedNode.next = null;
      removedNode.prev = null;
      this.length--;
      return removedNode;
    }
  }
  
  list = new DoublyLinkedList(12);
  
  list.push(99);
  list.push(100);
  list.push('LAST');
  // list.pop();
  // list.shift();
  list.unshift('Johnnnnnn');
  // console.log(list.get(2));
  console.log(list.remove(2));
  console.log(list);
  