-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerators.js
85 lines (63 loc) · 2.4 KB
/
generators.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
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
function* getEmployee() {
console.log('the function has started');
const names = ['Amanda', 'Diego', 'Farrin', 'James', 'Kagure', 'Kavita', 'Orit', 'Richard'];
for (const name of names) {
yield name;
}
console.log('the function has ended');
}
console.log(getEmployee());
const generatorIterator1 = getEmployee();
let result = generatorIterator1.next();
console.log(result.value, generatorIterator1.next().value, generatorIterator1.next());
function* udacity() {
yield 'Richard';
yield 'James'
}
const gener = udacity();
let result1 = gener.next().value;
console.log(result1, gener.next().value, gener.next().value);
// TASK 2:
function* getEmployee() {
const names = ['Amanda', 'Diego','Farrin'];
const facts = [];
for (const name of names) {
// yield *out* each name AND store the returned data into the facts array
facts.push(yield name);
}
return facts;
}
const start1 = getEmployee();
let getResult = start1.next().value;
getResult = start1.next(`${getResult} super`).value;
console.log(getResult);
getResult = start1.next(`${getResult} grt`).value;
const posit = start1.next(`${getResult} great!`).value;
console.log(posit);
function* getEmployee() {
const names = ['Amanda', 'Diego', 'Farrin', 'James', 'Kagure', 'Kavita', 'Orit', 'Richard'];
const facts = [];
for (const name of names) {
// yield *out* each name AND store the returned data into the facts array
facts.push(yield name);
}
return facts;
}
const generatorIterator = getEmployee();
// get the first name out of the generator
let name = generatorIterator.next().value;
// pass data in *and* get the next name
name = generatorIterator.next(`${name} is cool!`).value;
// pass data in *and* get the next name
name = generatorIterator.next(`${name} is awesome!`).value;
// pass data in *and* get the next name
name = generatorIterator.next(`${name} is stupendous!`).value;
// you get the idea
name = generatorIterator.next(`${name} is rad!`).value;
name = generatorIterator.next(`${name} is impressive!`).value;
name = generatorIterator.next(`${name} is stunning!`).value;
name = generatorIterator.next(`${name} is awe-inspiring!`).value;
// pass the last data in, generator ends and returns the array
const positions = generatorIterator.next(`${name} is magnificent!`).value;
// displays each name with description on its own line
console.log(positions.join('\n'));