forked from Adespinoza/daily-coding-problem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem_139.js
44 lines (37 loc) Β· 889 Bytes
/
problem_139.js
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
38
39
40
41
42
43
44
/* eslint max-classes-per-file: ["error", 2] */
class Iterator {
constructor(iterable = []) {
this.iterable = iterable;
this.index = 0;
}
next() {
const result = this.iterable[this.index];
this.index += 1;
return result;
}
hasNext() {
return this.iterable[this.index + 1] !== undefined;
}
}
class PeekableInterface extends Iterator {
constructor(iterable) {
super(iterable);
this.current = super.next();
}
peek() {
return this.current;
}
next() {
const prev = this.current;
this.current = super.next();
return prev;
}
}
// const peek1 = new PeekableInterface([1, 2, 3]);
// console.log(peek1);
// console.log(peek1.peek()); // 1
// console.log(peek1.hasNext()); // true
// console.log(peek1.next()); // 1
// console.log(peek1.peek()); // 2
// console.log(peek1.peek()); // 2
// console.log(peek1.next()); // 2