A little confused about the While condition in `doubly-linked-list.js` insertBefore and insertAfter

Author: lchtao26Created Feb 8, 2021Updated Oct 13, 2023

~/computer-science-in-javascript/src/data-structures/doubly-linked-list/doubly-linked-list.js

    insertBefore(data, index) {
            // ...omit other lines util here
            // line 183
            while ((current.next !== null) && (i < index)) {
                current = current.next;
                i++;
            }
        }
    insertAfter(data, index) {
        // ...omit other lines util here
        // line 259
        while ((current !== null) && (i < index)) {
            current = current.next;
            i++;
        }

I am confused about the difference of while condition between line 183 and 259,

and do a test in node for the insertAfter method:

const { DoublyLinkedListi } = require( "./doubly-linked-list.js");
const doublyLinkedList = new DoublyLinkedList();

doublyLinkedList.add(1);
doublyLinkedList.insertAfter(1, 1);

and it will throw an the error below, it's not a expected error throw by the doubly Linked List class.

            current.next.previous = newNode;
                    ^
TypeError: Cannot read property 'next' of null

So I think current.next !== null in insertBefore method is right for insertAfter, instead of current !== null

Source: humanwhocodes/computer-science-in-javascript