Created
September 2, 2018 12:59
-
-
Save Margino/3f20edd10e3887ccf088042d766ca290 to your computer and use it in GitHub Desktop.
Function that returns each value of the array when called, one element at a time
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function returnIterator(arr) { | |
| let i = 0; | |
| function inner() { | |
| const element = arr[i]; | |
| i++; | |
| return element; | |
| } | |
| return inner; | |
| } | |
| const array2 = ['a', 'b', 'c', 'd']; | |
| const myIterator = returnIterator(array2); | |
| console.log(myIterator()); // -> should log 'a' | |
| console.log(myIterator()); // -> should log 'b' | |
| console.log(myIterator()); // -> should log 'c' | |
| console.log(myIterator()); // -> should log 'd' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment