-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathproblem_166.js
39 lines (35 loc) Β· 1022 Bytes
/
problem_166.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
/* eslint "no-restricted-syntax": ["error", "ForInStatement", "LabeledStatement", "WithStatement"]
*/
class ArrayIterator {
constructor(list) {
this.array = list.reduce((a, b) => a.concat(b), []);
this.index = 0;
}
/**
* eturns the next element in the array of arrays.
* If there are no more elements, raise an exception.
* @return {number}
*/
next() {
return this.array[this.index++];
}
/**
* Returns whether or not the iterator still has elements left.
* @return {boolean}
*/
hasNext() {
return this.index < this.array.length;
}
}
const arr = new ArrayIterator([[1, 2], [3], [], [4, 5, 6]]);
console.log(arr.next()); // 1
console.log(arr.next()); // 2
console.log(arr.next()); // 3
console.log(arr.next()); // 4
console.log(arr.next()); // 5
console.log(arr.next()); // 6
console.log(arr.next()); // undefined
console.log(arr.next()); // undefined
console.log(arr.next()); // undefined
console.log(arr.next()); // undefined
console.log(arr.next()); // undefined