Skip to content

Commit 455bd6a

Browse files
committed
Merge pull request careercup#38 from DavidOrchard/javascript-chapter2-question3
Javascript chapter 2 question 3
2 parents 8f0afbd + 9ce70a0 commit 455bd6a

2 files changed

Lines changed: 51 additions & 0 deletions

File tree

  • javascript
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module.exports = LinkedList = (function() {
2+
var deleteNode = function(node) {
3+
if(!node || !node.next) return;
4+
var next = node.next;
5+
node.data = next.data
6+
node.next = next.next;
7+
};
8+
9+
return {
10+
deleteNode: deleteNode
11+
}
12+
}());
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
require('../../test_helper');
2+
describe(LinkedList, function () {
3+
describe('deleteNode)', function () {
4+
5+
it('runs if 0 length list', function () {
6+
var l = new SinglyLinkedList();
7+
LinkedList.deleteNode(l, new Node("foo"));
8+
expect(l).to.be.not.null;
9+
});
10+
11+
12+
it('deletes 1st item in 2 length list', function () {
13+
var l = new SinglyLinkedList();
14+
var n = new Node("foo");
15+
l.addBottom(n);
16+
l.addBottom(new Node("bar"));
17+
LinkedList.deleteNode(n);
18+
expect(l.head).to.equal(n);
19+
expect(l.head.data).to.equal("bar");
20+
});
21+
22+
it('returns 3rd item in 5 length list', function () {
23+
var l = new SinglyLinkedList();
24+
l.addBottom(new Node("a"));
25+
l.addBottom(new Node("b"));
26+
var n = new Node("c");
27+
l.addBottom(n);
28+
l.addBottom(new Node("d"));
29+
l.addBottom(new Node("e"));
30+
31+
LinkedList.deleteNode(n);
32+
expect(l.head).to.be.not.null;
33+
expect(l.head.data).to.equal("a");
34+
expect(l.head.next.data).to.equal("b");
35+
expect(l.head.next.next.data).to.equal("d");
36+
expect(l.head.next.next.next.data).to.equal("e");
37+
});
38+
});
39+
});

0 commit comments

Comments
 (0)