Created
October 3, 2018 20:41
-
-
Save AndrewGardhouse/3cd797d331ce3ea48efab931b7367d63 to your computer and use it in GitHub Desktop.
Write a function that uses recursion to return the total value of a generator
This file contains 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 * gen() { | |
for (let i = 0 ; i < 5; i++) yield i; | |
} | |
const myGen = gen() | |
const getTotal = (gen) => { | |
let total = 0; | |
const add = (gen) => { | |
const currentGen = gen.next(); | |
if (!currentGen.done) { | |
total += currentGen.value; | |
return add(gen); | |
} | |
return total; | |
} | |
return add(gen); | |
}; | |
console.log(getTotal(myGen)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment