-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakerLoop.js
More file actions
35 lines (28 loc) · 816 Bytes
/
makerLoop.js
File metadata and controls
35 lines (28 loc) · 816 Bytes
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
/*
The makeLooper() function takes a string (of non-zero length) as an argument.
It returns a function. The function it returns will return successive characters
of the string on successive invocations. It will start back at the beginning of the string
once it reaches the end.
For example:
var abc = makeLooper('abc');
abc(); // should return 'a' on this first call
abc(); // should return 'b' on this second call
abc(); // should return 'c' on this third call
abc(); // should return 'a' again on this fourth call
*/
function makeLooper(str) {
var result = {};
result.next = next;
result.str = str;
result.index = 0;
return result;
}
function next(){
console.log(this)
if(this.index === this.str.length){
this.index=0;
}
this.string = this.str[this.index];
this.index++;
return this.string;
}