From 456d065efa12101c6b755ae22aafb5bb7c82c4d2 Mon Sep 17 00:00:00 2001 From: slantz Date: Sat, 24 Oct 2015 23:11:56 +0300 Subject: [PATCH] Removing last element and referencing of next --- Chapter6/chap6-4.txt | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Chapter6/chap6-4.txt b/Chapter6/chap6-4.txt index 4a78c04..95eb57f 100644 --- a/Chapter6/chap6-4.txt +++ b/Chapter6/chap6-4.txt @@ -35,7 +35,10 @@ function remove(item) { var currNode = this.find(item); if (!(currNode.next == null)) { currNode.previous.next = currNode.next; - currNode.next.previous = currNode.previous; + // if we try to remove last element in the list via this method it will fail as previous of null. + if (current.next !== null) { + currNode.next.previous = currNode.previous; + } currNode.next = null; currNode.previous = null; } @@ -72,6 +75,10 @@ function insert(newElement, item) { var current = this.find(item); newNode.next = current.next; newNode.previous = current; + // if there is next element than it should have previous reference to current one or else it would preserve reference to it's previous element in th list + if (newNode.next) { + newNode.next.previous = newNode; + } current.next = newNode; }