2

The for loop is not returning the increasing count variable.

It acts as though the range function is not being called, but it is.

function* range (limit, count = 0) {
  if (count >= limit) return
  yield count
  range(limit, count + 1) 
} 


for (let i of range(16)) {
  console.log(i)
}

2

1 Answer 1

4

You need to yield * the range from your generator function.

Give this a try:

function* range (limit, count = 0) {
  if (count >= limit) return
  yield count
  yield * range(limit, count + 1) 
} 


for (let i of range(16)) {
  console.log(i)
}

Because it's recursive you need to yield the result back up to the parent basically.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.