-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsolution.js
More file actions
37 lines (32 loc) · 748 Bytes
/
solution.js
File metadata and controls
37 lines (32 loc) · 748 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const mainStack = [];
const helperStack = [];
/**
*
* @param {[any]} from
* @param {[any]} to
*/
function transferItems(from, to) {
while (from.length > 0) {
const removed = from.pop();
to.push(removed);
}
}
function enqueue(val) {
mainStack.push(val);
return enqueue; // for chaining calls
}
function dequeue() {
transferItems(mainStack, helperStack);
const removed = helperStack.pop();
transferItems(helperStack, mainStack);
return removed;
}
// Test
enqueue(1)(2)(3)(4)(5)(6);
console.log(dequeue()); // 1
console.log(dequeue()); // 2
console.log(dequeue()); // 3
console.log(dequeue()); // 4
console.log(dequeue()); // 5
console.log(dequeue()); // 6
console.log(dequeue()); // undefined