-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathas18.js
More file actions
133 lines (81 loc) · 4.09 KB
/
as18.js
File metadata and controls
133 lines (81 loc) · 4.09 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// 1. What is the difference between a generator and a function?
// a function and a generator are both used to define a block of code that can be executed at a later time. However, there are several key differences between these two constructs:
// Return values: A function returns a single value using the return statement, whereas a generator can return multiple values using the yield keyword.
// Execution: A function is executed from start to finish, whereas a generator can be paused and resumed at different points in its execution using the yield keyword.
// This allows a generator to return multiple values over time, rather than all at once.
// Iteration: A generator can be used to create an iterator object, which can be used to iterate over a series of values produced by the generator.
// In contrast, a function does not have this capability.
// Memory usage: A generator is more memory-efficient than a function, as it only generates values when they are requested,
// rather than generating all values at once and storing them in memory.
// In summary, a generator is a special type of function that allows you to return multiple values over time and can be paused and resumed during its execution.
// It is also more memory-efficient than a regular function.
// 2. What is the syntax of a generator?
// function *disp(){
// console.log("1");
// yield '1st Pause'
// console.log("2");
// yield '2nd Pause'
// console.log("3");
// yield '3rd Pause'
// console.log("4");
// yield '4th Pause'
// }
// let control = disp()
// control.next()
// control.next()
// 3. Are function generators iterable in JavaScript?
// Yes, function generators are iterable in JavaScript. A function generator can be used to create an iterator object,
// which can be used to iterate over a series of values produced by the generator.
// To create an iterator object from a function generator, you can use the built-in Symbol.iterator method,
// which returns an object with a next() method that produces the next value in the generator sequence.
// Here is an example:
// function* myGenerator() {
// yield 1;
// yield 2;
// yield 3;
// }
// const iterator = myGenerator()[Symbol.iterator]();
// console.log(iterator.next()); // { value: 1, done: false }
// console.log(iterator.next()); // { value: 2, done: false }
// console.log(iterator.next()); // { value: 3, done: false }
// console.log(iterator.next()); // { value: undefined, done: true }
// In this example, myGenerator() is a function generator that produces a sequence of values using the yield keyword.
// The iterator object is created by calling the Symbol.iterator method on the generator,
// which returns an object with a next() method that can be used to iterate over the sequence.
// When the next() method is called on the iterator object,
// it returns an object with a value property that contains the next value in the sequence,
// and a done property that indicates whether the end of the sequence has been reached.
// 4. Create a generator for multiplying?
// Output -
// function* multiply(a,b){
// yield a*b;
// }
// const result = multiply(2,2).next().value;
// console.log(result);
// 5. Print an infinite series of natural numbers using a generator.
// function* naturalNumbers() {
// let i = 1;
// while (true) {
// yield i++;
// }
// }
// const iterator = naturalNumbers();
// console.log(iterator.next().value); // 1
// console.log(iterator.next().value); // 2
// console.log(iterator.next().value); // 3
// 6. Create a generator that can throw an exception.
// function* myGenerator() {
// try {
// yield 1;
// yield 2;
// throw new Error('Generator error');
// yield 3;
// } catch (err) {
// yield err.message;
// }
// }
// const iterator = myGenerator();
// console.log(iterator.next()); // { value: 1, done: false }
// console.log(iterator.next()); // { value: 2, done: false }
// console.log(iterator.next()); // { value: 'Generator error', done: false }
// console.log(iterator.next()); // { value: undefined, done: true }