forked from maulanaxreza/Roblox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctiongenerator.html
More file actions
61 lines (54 loc) · 1.59 KB
/
Copy pathfunctiongenerator.html
File metadata and controls
61 lines (54 loc) · 1.59 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
//contoh 1
function * createName(){
yield "Maulana"
yield "Ahmad"
yield "Fahreza"
}
document.writeln(createName())
const names = createName()
for(let nama of names){
document.writeln(nama)
console.log(nama);
}
//contoh 2
//lazy generator : dia akan mengekskusi console log dulu di baris 29 kemudian masuk ke 26 kemudian kembali lagi ke looping
//berbeda dengan array dimana dia akan menyelesaikan looping dahulu kemudian menyimpan dalam array kosong dan masuk ke baris 26
function * buatGanjil(value){
for(let i = 1; i<=value; i++){
if ( i % 2 === 1 ){
console.log(`yield ${i}`);
yield i
}
}
}
const numbers = buatGanjil(100)
for (const i of numbers){
console.log(i);
}
function buatGanjilArray(value){
const array=[]
for(let i = 1; i<=value; i++){
if ( i % 2 === 1 ){
console.log(`yield ${i}`);
array.push(i)
}
}
return array
}
const nomor = buatGanjilArray(100)
for (const i of nomor){
console.log(i);
}
</script>
</body>
</html>