Skip to content

Commit 34a8b70

Browse files
author
Rail
committed
change from ES5 to ES6
1 parent d272b1e commit 34a8b70

File tree

1 file changed

+13
-13
lines changed

1 file changed

+13
-13
lines changed

Data-Structures/Queue/Queue.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
// Functions: enqueue, dequeue, peek, view, length
99

10-
var Queue = (function () {
10+
const Queue = ( function () {
1111
// constructor
1212
function Queue () {
1313
// This is the array representation of the queue
@@ -17,7 +17,7 @@ var Queue = (function () {
1717
// methods
1818
// Add a value to the end of the queue
1919
Queue.prototype.enqueue = function (item) {
20-
this.queue[this.queue.length] = item
20+
this.queue.push(item)
2121
}
2222

2323
// Removes the value at the front of the queue
@@ -26,7 +26,7 @@ var Queue = (function () {
2626
throw new Error('Queue is Empty')
2727
}
2828

29-
var result = this.queue[0]
29+
let result = this.queue[0]
3030
this.queue.splice(0, 1) // remove the item at position 0 from the array
3131

3232
return result
@@ -51,7 +51,7 @@ var Queue = (function () {
5151
}())
5252

5353
// Implementation
54-
var myQueue = new Queue()
54+
const myQueue = new Queue()
5555

5656
myQueue.enqueue(1)
5757
myQueue.enqueue(5)
@@ -62,19 +62,19 @@ myQueue.enqueue(54)
6262

6363
myQueue.view()
6464

65-
console.log('Length: ' + myQueue.length())
66-
console.log('Front item: ' + myQueue.peek())
67-
console.log('Removed ' + myQueue.dequeue() + ' from front.')
68-
console.log('New front item: ' + myQueue.peek())
69-
console.log('Removed ' + myQueue.dequeue() + ' from front.')
70-
console.log('New front item: ' + myQueue.peek())
65+
console.log(`Length: ${myQueue.length()}`)
66+
console.log(`Front item: ${myQueue.peek()}`)
67+
console.log(`Removed ${myQueue.dequeue()} from front.`)
68+
console.log(`New front item: ${myQueue.peek()}`)
69+
console.log(`Removed ${myQueue.dequeue()} from front.`)
70+
console.log(`New front item: ${myQueue.peek()}`)
7171
myQueue.enqueue(55)
7272
console.log('Inserted 55')
73-
console.log('New front item: ' + myQueue.peek())
73+
console.log(`New front item: ${myQueue.peek()}`)
7474

75-
for (var i = 0; i < 5; i++) {
75+
for (let i = 0; i < 5; i ++) {
7676
myQueue.dequeue()
7777
myQueue.view()
7878
}
7979

80-
// console.log(myQueue.dequeue()); // throws exception!
80+
// console.log(myQueue.dequeue()); // throws exception!

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy