Skip to content

Commit b478170

Browse files
committed
bugfix: handled acessing last element of an empty queue
1 parent d67fdf6 commit b478170

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

round-linked-queue.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ class RoundLinkedQueue {
2525
}
2626

2727
get last() {
28+
if (!this._last) {
29+
throw new Error("Cannot access the last element of an empty queue");
30+
}
31+
2832
return this._last.data;
2933
}
3034

round-linked-queue.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,5 +122,11 @@ describe("Round-Queue", () => {
122122

123123
expect(() => queue.first).to.throw("Cannot access the first element of an empty queue");
124124
});
125+
126+
it("Should throw a proper error when acessing the last element of an empty queue", () => {
127+
const queue = new RoundQueue(3);
128+
129+
expect(() => queue.last).to.throw("Cannot access the last element of an empty queue");
130+
});
125131
});
126132
});

0 commit comments

Comments
 (0)