Skip to content

Instantly share code, notes, and snippets.

@AndrewGardhouse
Created October 3, 2018 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndrewGardhouse/3cd797d331ce3ea48efab931b7367d63 to your computer and use it in GitHub Desktop.
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
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