Web crashes when I run tests

This page is having some difficulties

status:Error_BREAKPOINT

I am working on doubly-linked lists challenge.

What is your code? What is the link to the challenge?

Sometimes logical errors in your code can create issues that crash the browser.

var Node = function (data, prev) {
  this.data = data;
  this.prev = prev;
  this.next = null;
};
var DoublyLinkedList = function () {
  this.head = null;
  this.tail = null;
  // Only change code below this line
  this.add = function (element) {
    let node = new Node(element, null);
    if (this.head == null) {
      this.head = node;
      this.tail = node;
    } else {
      let current = this.head;
      while (current.next) {
        current = current.next;
      }

      current.next = node;
    }

    if (this.tail == null) {
      this.tail = node;
    } else {
      let prevNode = this.tail;
      this.tail = node;
      this.tail.prev = prevNode;
    }
  };
  this.remove = function (element) {
    if (this.head == null) {
      return null;
    }
    console.log(this.head, this.tail);
    if (element == this.head.data && element == this.tail.data) {
      this.head = null;
      this.tail = null;
    } else if (element == this.head.data) {
      this.head = this.head.next;
    } else if (element == this.tail.data) {
      this.tail = this.tail.prev;
    }

    let index = 0;
    let previousNode;
    let currentNode = this.head;

    while (currentNode.next != null) {
      previousNode = currentNode;
      currentNode = currentNode.next;
      index++;
      if (currentNode.data == element) {
        previousNode.next = currentNode.next;
        currentNode.next.prev = previousNode;
        return;
      }
    }
    
  };

  // Only change code above this line
};

This is the code I tested it locally but only a little. There weren’t many changes in my code and it worked.

Link to the challenge create-a-doubly-linked-list

I would 1) comment out the console log statements for the tests and 2) make sure your browser is up to date.

Tried in different browser and works.

1 Like