-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathtest4.js
More file actions
61 lines (54 loc) · 1.11 KB
/
Copy pathtest4.js
File metadata and controls
61 lines (54 loc) · 1.11 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
const infiniteNumberIterator = {
current: 1,
next() {
return { value: this.current++, done: false };
}
};
function* evenNumberGenerator() {
let num = 0;
while (true) {
yield num;
num += 2;
}
}
function* fibonacciGenerator() {
let a = 0, b = 1;
while (true) {
yield a;
[a, b] = [b, a + b];
}
}
const customIterable = {
n: 7,
[Symbol.iterator]() {
let i = 1;
let n = this.n;
return {
next() {
if (i <= n) {
return { value: i++, done: false };
} else {
return { done: true };
}
}
};
}
};
console.log("First 5 numbers from infinite iterator:");
for (let i = 0; i < 5; i++) {
console.log(infiniteNumberIterator.next().value);
}
console.log("First 5 even numbers:");
const evenGen = evenNumberGenerator();
for (let i = 0; i < 5; i++) {
console.log(evenGen.next().value);
}
console.log("First 7 Fibonacci numbers:");
const fibGen = fibonacciGenerator();
for (let i = 0; i < 7; i++) {
console.log(fibGen.next().value);
}
console.log("Output of Custom Iterable:");
for (let num of customIterable) {
console.log(num);
}