-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay15iterables.html
66 lines (58 loc) · 1.7 KB
/
Day15iterables.html
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
<!DOCTYPE html>
<html>
<head>
<title>
Day15
</title>
</head>
<body>
<script>
//iterator in javascript
let numbers=[1,2,3,4];// arrays are normally iterable
let range={ // objects are not iterable by default
form:1,
to:5,
[Symbol.iterator](){
this.current=this.form
return this;
},
next(){
if(this.current<=this.to){
return{value:this.current++,done:false};
}
else {
return{done:true}
}
}
};
for(let num of range){
console.log(num)
}
let names={
fname:'ponmani',
lname:'venktesan'
}
names[Symbol.iterator]=function(){
let properties = Object.keys(names);
let count=0;
// let isDone = false;
let next = () => {
// if(count>=properties.length){
// isDone=true;
// }
// return{done:isDone, value:this[properties[count++]]};
if(count<properties.length){
return{done:false, value:this[properties[count++]]};
}
else{
return{done:true};
}
}
return{next};
};
for(let name of names){
console.log(name);
}
</script>
</body>
</html>