All right, I hope you gave this challenge a good go before coming over to the solution. And it's because 1
you only really learn when you try and fail and go through challenges like this. 2
So if you haven't given enough time, stop. 3
Think about it. 4
Wait. Come back to it. 5
And only if you get really stuck, then come over here and check through the solution with me. 6
Now, I'm going to be working off the flowchart that I showed you previous to the challenge. 7
So we're going to be converting all of this logic into code. 8
So to begin, we start out with an output that's equal to an empty array. 9
So let's write that in our code. 10
So let's create some sort of variable called output and we can set that to an empty array with nothing 11
inside. 12
Now, the next step is we have to check whether if n is equal to 1. And if so, we're going to give 13
the output as 0. 14
So to do that, we'll use an if statement to check if this n that's being passed in is equal to 1. 15
And if so, then we're going to say that output is going to be equal to just [0]. 16
So this is the first item in the sequence and then we can go ahead and return the output. 17
Now, the next step in our flowchart is, well, if it wasn't equal to 1, then is it equal to 2? 18
And if so, then the output should be [0, 1]. 19
So we can represent this logic by using an else if. Remember that else if only gets checked if the first 20
one was false. 21
So now we can check 22
well, if it's not equal to 1, well, is it equal to 2? 23
So if it is, then we're going to set the output to equal [0, 1] stored inside an array, and 24
again, it will be returned at the end. 25
Now, finally, if it's not 1 and it's not 2, well, then we have to sum the last two values in 26
the output. So we can catch that final condition using an else statement. 27
And inside here, we're going to set the output to equal to [0, 1] to begin, because this is 28
the start of our sequence, 29
and then we're going to sum the last two values. 30
And we can do that by reaching into the outputs and getting hold of the first value, 31
so output [0], which is this one. And then we add that to the output[1]. 32
So now we're basically adding this first item to the second item and this should equal 1, and that 33
should be somehow added to the end of this existing array. 34
And if you remember from previous lessons on arrays, we can do that by saying output.push. And 35
we can wrap this calculation inside parentheses and we will end up adding this solution to the existing 36
array. 37
And at this point, the output should now look like [0, 1, 1] and it will get returned. 38
Now we have to check whether if n equals the number of items in the output. 39
So we could do this using another if statement, and we could say if n triple equals output length, then 40
we can go ahead and return the output. 41
But otherwise, we have to continue to add the last two items together. 42
So how could we change our code here so that instead of manually saying it's the item from the 43
output array position 0, this one, plus the item at position 1, this one. 44
How can we say instead that we want to get the last item plus the second from the last item? 45
Well, we could use the length. 46
So in this case, output.length is going to be equal to 2. 47
So if we wanted this to be 1, then we could say output.length 48
- 1, and then we have our second from the end, which is going to be output.length - 2. 49
So this still works exactly the same 50
but now this line of code can work no matter the size of our array. Because even if it was this long, 51
output.length at this point is going to be equal to 4. 52
So 4 - 1 is going to be 3 53
so this becomes 3. 54
And if we look at the item at position 3 in our array, it's 0, 1, 2, 3 55
so it's this last item. And - 2 makes it 2, and 0, 1, 2 becomes the second from the last item. 56
So we're adding 1 + 2 here. 57
So this line of code now makes it dynamic and we could now use it inside our else statement. 58
But notice how these two lines of code are now repeating. 59
And also when we get to the end of the else statement, we have no way of going back to the beginning to 60
check if the n is equal to output.length 61
again, like what is required here. 62
Given how much this looks like a circle, it should remind you that we need to use a loop. 63
So instead of writing all of this, we could just simply create a loop. 64
And the type of loop that I'm going to create in this case is a for loop. 65
So I'm going to say let's create a variable inside the for loop that's set to equal 2, so the existing 66
number of items in our output. 67
And then we're going to use a semicolon and say that while i is less than n, the total number of items 68
we need in our output, continue to increase i by 1. And every single time 69
what you want to do is to do this to get the last item from the outputs, to get the second from the 70
last item from the output, add them together and then push it onto our array. And finally return the 71
output. 72
So now let's take our fibonacciGenerator and then paste it into our Repl.it playground. 73
And now let's go ahead and try to run our code by calling the fibonacciGenerator. 74
And let's just start off with something quite simple. 75
Let's start with n = 1. 76
Now, if I hit run in the output, I get [0]. 77
If I change this to 2 in the output, I get [0, 1]. 78
And if I change this to 5 in the output, I get a five-item sequence in an array. 79
So now that we've confirmed that our code works, let's go ahead and click check solution to see if 80
we got it right. 81
Brilliant. 82
So now we've passed this challenge. 83
How did you get on with this challenge? 84
Did you struggle with maybe using some of the array methods or did you remember to use a loop so that 85
it goes around and around and does the same action repeatedly? 86
Now, remember that there's many, many ways of solving this challenge. 87
You could have used a while loop, 88
you could have done something fancy instead of using many ifs and else ifs. 89
There's a lot of ways. 90
But as long as your output satisfies the criteria that we set out in the challenge, then it doesn't 91
matter which way you chose. 92
In fact, at this stage, as long as your solution makes sense to you, then that will be the perfect 93
solution.