April 2018
Beginner to intermediate
426 pages
10h 19m
English
Next, we are going to implement the dequeue method. This method is responsible for removing items from the queue. As the queue uses the FIFO principle, the first item that we added in the queue is the one that is removed:
dequeue() { if (this.isEmpty()) { return undefined; } const result = this.items[this.lowestCount]; // {1} delete this.items[this.lowestCount]; // {2} this.lowestCount++; // {3} return result; // {4}}
First, we need to verify whether the queue is empty and, if so, we return the value undefined. If the queue is not empty, we will store the value from the front of the queue ({1}) so we can return it ({4}) after the element has been removed ({2}). We also need to increase the lowestCount property ...