Check out a free preview of the full TypeScript Fundamentals course:
The "Challenge 6: Solution" Lesson is part of the full, TypeScript Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Mike walks through the solution to Challenge 6.

Get Unlimited Access Now

Transcript from the "Challenge 6: Solution" Lesson

>> Mike North: So we're gonna go through the process of building a Fibonacci generator. So we're gonna be working within this function here, it's generator function that it is meant to implement the Fibonacci sequence. So if we run our tests,
>> Mike North: We can see that only one thing is exported from the module, so this test is happy.

[00:00:31] Get fib sequence returns in iterator, we really have no other option when working with generator functions. We also return an interator. But then you can see that things sort of break down from there in that we're basically, we're getting undefined as values off of this iterator meaning it is done to begin with, there's nothing here.

>> Mike North: So let's get started. So using this idea of the diagonals convention that I explained before, we can say twoAgo. This is a better name than last class. TwoAgo equals one.
>> Mike North: oneAgo = 0; while(true) next value is gonna be twoAgo plus oneAgo.
>> Mike North: And then, we can yield that out.

[00:01:34] And then, our job is basically to because this closure is preserved. When we ask for the next number, we kind of have to update things based on like we have to shuffle our numbers around till we are ready for the next item in the sequence so in this case, we'll say twoago = oneago, oneago = nextValue, and we are ready to turn over the loop again right so,

>> Mike North: Just thinking through this It would be and I could say if I can just pass, great. So the tests pass. If we just walk through this code we start with zero and one, right, and then we're going to calculate the next value and in this case it's going to be one.

[00:02:27] Zero plus one is one, we'll yield that out. So it's our first number in the sequence.
>> Mike North: Right? Then the next time we pull something off of the iterator, we're gonna go down here. This will still be one, right? So we'll say,
>> Mike North: We'll basically say that the old.

[00:02:50] All right, so twoAgo gets the previous value for oneAgo. This will get the one that we just returned, right. And then, we'll turn around the loop and go back up here, an 0 + 1 is also 1.
>> Mike North: And then, the next time we pull the iterator around Twoago is gonna be one.

[00:03:13] Oneago is gonna be one. Cause that's what we just returned, and then, we're gonna get two and so on. So this is like in terms of making your code more expressive, this is a pretty expressive way to describe the nudgy algorithm. Right. It doesn't involve explicitly creating additional closures.

[00:03:39] This construct of being able to emit numbers one by one gives us a new way to represent this. Once you understand the way yield works and the way this generator function pauses execution at yield, it is a convenient tool to use for this kind of thing.