Create a Doubly Linked List TypeError: Converting circular structure to JSON

Create a Doubly Linked List

Is something wrong at /js/test-evaluator.js:569?
While I try to execute my code, I get this error on console 40 times:

TypeError: Converting circular structure to JSON
–> starting at object with constructor ‘Node’
| property ‘next’ -> object with constructor ‘Node’
— property ‘prev’ closes the circle

My code:

var Node = function(data, prev) {
    this.data = data;
    this.prev = prev;
    this.next = null;
};

var DoublyLinkedList = function() {
    this.head = null;
    this.tail = null;
    
    // change code below this line
    this.add = function(data) {
        if (!(data)) return;
        const node = new Node(data);
        if (!(this.head)) {
            this.head = this.tail = node;
        } else {
            this.tail.next = node;
            node.prev = this.tail;
        }
        this.tail = node;
    };
    
    this.remove = function(data) {
        if (!(data)) return;
        let currentNode = this.head;
        if (currentNode.data === data) {
            this.head = currentNode.next;
            this.head.prev = null;
            return;
        }
        if (this.tail.data === data) {
            this.tail.prev.next = null;
            return;
        }
        while (currentNode !== null) {
            if (currentNode.data === data) {
                if (currentNode.next) {
                    currentNode.next.prev = currentNode.prev;
                    currentNode.prev.next = currentNode.next;
                }
            }
            currentNode = currentNode.next;
        }
        return null;
    };
    // change code above this line
};

const myDoublyList = new DoublyLinkedList();
myDoublyList.add("andi");
myDoublyList.add("csaba");
myDoublyList.add("edina");
myDoublyList.add("kriszta");
myDoublyList.remove("kriszta");
console.log(myDoublyList.head);

All test fails… :frowning:
On localhost everything works fine…

1 Like

When I run the tests for this challenge with the code you supplied, the only test which fails is the following:

Removing an item from an empty list returns null.

Actually, it is the last console.log in your code which causes all the tests to fail. If you remove that last console.log and run the test, look at your browser console to see you have circular references, which the browser does not like. Node shows them but does not complain.

Thank you,
I changed the if statement (… || !(this.head)) return null);
I found another bug too: when only one node is on the list… fixed.

I have same error again…

var Node = function(data, prev) {
    this.data = data;
    this.prev = prev;
    this.next = null;
};

var DoublyLinkedList = function() {
    this.head = null;
    this.tail = null;
    
    // change code below this line
    this.add = function(data) {
        if (!(data)) return;
        const node = new Node(data);
        if (!(this.head)) {
            this.head = this.tail = node;
        } else {
            this.tail.next = node;
            node.prev = this.tail;
        }
        this.tail = node;
    };
    
    this.remove = function(data) {
        if (!(data) || !(this.head)) return null;
        let currentNode = this.head;
        if (currentNode.data === data) {
            if (this.tail.data === data) {
                this.head = null;
                return;
            }
            this.head = currentNode.next;
            this.head.prev = null;
            return;
        }
        if (this.tail.data === data) {
            this.tail.prev.next = null;
            return;
        }
        while (currentNode !== null) {
            if (currentNode.data === data) {
                if (currentNode.next) {
                    currentNode.next.prev = currentNode.prev;
                    currentNode.prev.next = currentNode.next;
                }
            }
            currentNode = currentNode.next;
        }
        return null;
    };
    
    this.reverse = function() {
        if (!(this.head)) return null;
        let tempNode = this.tail;
        while (tempNode.prev) {
            let next = tempNode.next;
            tempNode.next = tempNode.prev;
            tempNode.prev = next;
            tempNode = tempNode.next;
        }
        console.log(tempNode);
        tempNode.prev = tempNode.next;
        tempNode.next = null;
        this.head = this.tail;
        this.tail = tempNode;
    };
    // change code above this line
};

const myDoublyList = new DoublyLinkedList();
myDoublyList.add("andi");
myDoublyList.add("csaba");
//myDoublyList.add("edina");
//myDoublyList.add("kriszta");
//myDoublyList.remove("kriszta");
console.log(myDoublyList.reverse());
console.log(myDoublyList.head);

test-evaluator.js:569 TypeError: test.add is not a function
at eval (eval at _callee$ (test-evaluator.js:569), :1:125)
at eval (eval at _callee$ (test-evaluator.js:569), :1:231)
at _callee$ (test-evaluator.js:569)
at x (test-evaluator.js:68)
at Generator._invoke (test-evaluator.js:68)
at Generator.E.forEach.t. [as next] (test-evaluator.js:68)
at n (test-evaluator.js:58)
at s (test-evaluator.js:58)
at test-evaluator.js:58
at new Promise ()