WEBVTT 0 00:00.390 --> 00:06.540 All right, I hope you gave this challenge a good go before coming over to the solution. And it's because 1 00:06.540 --> 00:11.750 you only really learn when you try and fail and go through challenges like this. 2 00:12.180 --> 00:15.150 So if you haven't given enough time, stop. 3 00:15.270 --> 00:16.070 Think about it. 4 00:16.230 --> 00:17.880 Wait. Come back to it. 5 00:18.300 --> 00:24.290 And only if you get really stuck, then come over here and check through the solution with me. 6 00:24.690 --> 00:30.450 Now, I'm going to be working off the flowchart that I showed you previous to the challenge. 7 00:30.780 --> 00:34.650 So we're going to be converting all of this logic into code. 8 00:35.220 --> 00:40.180 So to begin, we start out with an output that's equal to an empty array. 9 00:40.860 --> 00:42.690 So let's write that in our code. 10 00:43.050 --> 00:50.400 So let's create some sort of variable called output and we can set that to an empty array with nothing 11 00:50.400 --> 00:50.970 inside. 12 00:51.750 --> 00:57.480 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 00:57.480 --> 00:59.270 the output as 0. 14 00:59.820 --> 01:08.550 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 01:09.090 --> 01:16.600 And if so, then we're going to say that output is going to be equal to just [0]. 16 01:16.860 --> 01:23.640 So this is the first item in the sequence and then we can go ahead and return the output. 17 01:24.330 --> 01:30.840 Now, the next step in our flowchart is, well, if it wasn't equal to 1, then is it equal to 2? 18 01:31.380 --> 01:35.390 And if so, then the output should be [0, 1]. 19 01:35.940 --> 01:44.100 So we can represent this logic by using an else if. Remember that else if only gets checked if the first 20 01:44.100 --> 01:45.600 one was false. 21 01:46.290 --> 01:47.730 So now we can check 22 01:47.740 --> 01:51.000 well, if it's not equal to 1, well, is it equal to 2? 23 01:51.690 --> 01:59.940 So if it is, then we're going to set the output to equal [0, 1] stored inside an array, and 24 01:59.940 --> 02:02.490 again, it will be returned at the end. 25 02:03.240 --> 02:10.650 Now, finally, if it's not 1 and it's not 2, well, then we have to sum the last two values in 26 02:10.650 --> 02:16.250 the output. So we can catch that final condition using an else statement. 27 02:16.800 --> 02:24.780 And inside here, we're going to set the output to equal to [0, 1] to begin, because this is 28 02:24.780 --> 02:26.220 the start of our sequence, 29 02:26.790 --> 02:30.030 and then we're going to sum the last two values. 30 02:30.540 --> 02:37.340 And we can do that by reaching into the outputs and getting hold of the first value, 31 02:37.350 --> 02:46.680 so output [0], which is this one. And then we add that to the output[1]. 32 02:47.550 --> 02:56.190 So now we're basically adding this first item to the second item and this should equal 1, and that 33 02:56.190 --> 03:00.290 should be somehow added to the end of this existing array. 34 03:00.900 --> 03:07.560 And if you remember from previous lessons on arrays, we can do that by saying output.push. And 35 03:07.560 --> 03:16.380 we can wrap this calculation inside parentheses and we will end up adding this solution to the existing 36 03:16.380 --> 03:16.860 array. 37 03:17.160 --> 03:26.430 And at this point, the output should now look like [0, 1, 1] and it will get returned. 38 03:26.880 --> 03:32.240 Now we have to check whether if n equals the number of items in the output. 39 03:32.730 --> 03:41.640 So we could do this using another if statement, and we could say if n triple equals output length, then 40 03:41.640 --> 03:45.420 we can go ahead and return the output. 41 03:45.690 --> 03:52.830 But otherwise, we have to continue to add the last two items together. 42 03:53.100 --> 04:01.890 So how could we change our code here so that instead of manually saying it's the item from the 43 04:01.890 --> 04:09.540 output array position 0, this one, plus the item at position 1, this one. 44 04:10.020 --> 04:16.350 How can we say instead that we want to get the last item plus the second from the last item? 45 04:17.160 --> 04:19.700 Well, we could use the length. 46 04:20.040 --> 04:23.810 So in this case, output.length is going to be equal to 2. 47 04:24.330 --> 04:29.820 So if we wanted this to be 1, then we could say output.length 48 04:31.040 --> 04:40.160 - 1, and then we have our second from the end, which is going to be output.length - 2. 49 04:41.330 --> 04:43.640 So this still works exactly the same 50 04:44.060 --> 04:52.190 but now this line of code can work no matter the size of our array. Because even if it was this long, 51 04:52.520 --> 04:55.880 output.length at this point is going to be equal to 4. 52 04:56.390 --> 04:59.410 So 4 - 1 is going to be 3 53 04:59.780 --> 05:02.120 so this becomes 3. 54 05:02.570 --> 05:08.830 And if we look at the item at position 3 in our array, it's 0, 1, 2, 3 55 05:08.840 --> 05:18.220 so it's this last item. And - 2 makes it 2, and 0, 1, 2 becomes the second from the last item. 56 05:18.230 --> 05:20.220 So we're adding 1 + 2 here. 57 05:20.780 --> 05:28.060 So this line of code now makes it dynamic and we could now use it inside our else statement. 58 05:28.250 --> 05:32.020 But notice how these two lines of code are now repeating. 59 05:32.480 --> 05:39.230 And also when we get to the end of the else statement, we have no way of going back to the beginning to 60 05:39.230 --> 05:41.900 check if the n is equal to output.length 61 05:41.900 --> 05:44.380 again, like what is required here. 62 05:44.780 --> 05:51.050 Given how much this looks like a circle, it should remind you that we need to use a loop. 63 05:51.560 --> 05:56.570 So instead of writing all of this, we could just simply create a loop. 64 05:57.200 --> 06:01.460 And the type of loop that I'm going to create in this case is a for loop. 65 06:01.970 --> 06:09.080 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 06:09.080 --> 06:11.090 number of items in our output. 67 06:11.870 --> 06:21.230 And then we're going to use a semicolon and say that while i is less than n, the total number of items 68 06:21.230 --> 06:28.040 we need in our output, continue to increase i by 1. And every single time 69 06:28.430 --> 06:36.230 what you want to do is to do this to get the last item from the outputs, to get the second from the 70 06:36.230 --> 06:45.110 last item from the output, add them together and then push it onto our array. And finally return the 71 06:45.110 --> 06:45.620 output. 72 06:46.130 --> 06:52.240 So now let's take our fibonacciGenerator and then paste it into our Repl.it playground. 73 06:52.850 --> 06:58.820 And now let's go ahead and try to run our code by calling the fibonacciGenerator. 74 06:59.120 --> 07:01.160 And let's just start off with something quite simple. 75 07:01.160 --> 07:03.530 Let's start with n = 1. 76 07:04.280 --> 07:07.980 Now, if I hit run in the output, I get [0]. 77 07:08.810 --> 07:14.090 If I change this to 2 in the output, I get [0, 1]. 78 07:14.810 --> 07:23.480 And if I change this to 5 in the output, I get a five-item sequence in an array. 79 07:24.140 --> 07:30.290 So now that we've confirmed that our code works, let's go ahead and click check solution to see if 80 07:30.290 --> 07:30.980 we got it right. 81 07:31.400 --> 07:31.790 Brilliant. 82 07:31.820 --> 07:33.730 So now we've passed this challenge. 83 07:34.400 --> 07:36.160 How did you get on with this challenge? 84 07:36.560 --> 07:44.000 Did you struggle with maybe using some of the array methods or did you remember to use a loop so that 85 07:44.000 --> 07:47.610 it goes around and around and does the same action repeatedly? 86 07:48.230 --> 07:52.010 Now, remember that there's many, many ways of solving this challenge. 87 07:52.010 --> 07:53.660 You could have used a while loop, 88 07:54.020 --> 07:58.460 you could have done something fancy instead of using many ifs and else ifs. 89 07:58.910 --> 08:00.560 There's a lot of ways. 90 08:00.710 --> 08:06.950 But as long as your output satisfies the criteria that we set out in the challenge, then it doesn't 91 08:06.950 --> 08:08.410 matter which way you chose. 92 08:08.990 --> 08:15.290 In fact, at this stage, as long as your solution makes sense to you, then that will be the perfect 93 08:15.290 --> 08:15.950 solution.