Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,510 --> 00:00:03,120
All right I hope you gave this challenge a good go.
2
00:00:03,120 --> 00:00:05,230
Before coming over to the solution.
3
00:00:05,460 --> 00:00:12,210
And it's because you only really learn when you try and fail and go through challenges like this.
4
00:00:12,210 --> 00:00:16,290
So if you haven't given it enough time stop think about it.
5
00:00:16,290 --> 00:00:16,940
Wait.
6
00:00:16,950 --> 00:00:18,280
Come back to it.
7
00:00:18,390 --> 00:00:20,730
And only if you get really stuck.
8
00:00:20,820 --> 00:00:24,750
Then come over here and check through the solution with me.
9
00:00:24,750 --> 00:00:30,840
Now I'm going to be working off the flow chart that I showed you previous to the challenge.
10
00:00:30,840 --> 00:00:36,210
So we're going to be converting all of this logic into code so to begin.
11
00:00:36,210 --> 00:00:40,210
We start out with an output that's equal to an empty array.
12
00:00:40,920 --> 00:00:43,050
So let's write that in our code.
13
00:00:43,050 --> 00:00:50,400
So let's create some sort of variable code output and we can set that to an empty array with nothing
14
00:00:50,400 --> 00:00:51,780
inside.
15
00:00:51,780 --> 00:00:58,200
Now the next step is we have to check whether if n is equal to 1 and if so we're going to give the output
16
00:00:58,350 --> 00:00:59,280
as 0.
17
00:00:59,850 --> 00:01:08,920
So to do that we'll use an if statement to check if this n that's being passed in is equal to 1.
18
00:01:09,210 --> 00:01:16,920
And if so then we're going to say that output is going to be equal to just zero.
19
00:01:16,920 --> 00:01:24,380
So this is the first item in the sequence and then we can go ahead and return the output.
20
00:01:24,390 --> 00:01:31,420
Now the next step in our flowchart is will if it wasn't equal to 1 then is it equal to 2.
21
00:01:31,470 --> 00:01:35,870
And if so then the output should be zero comma 1.
22
00:01:36,000 --> 00:01:44,070
So we can represent this logic by using an elusive remember that else if only gets checked if the first
23
00:01:44,070 --> 00:01:46,340
one was false.
24
00:01:46,350 --> 00:01:51,100
So now we can say well if it's not equal to 1 Well is it equal to 2.
25
00:01:51,720 --> 00:02:00,420
So if it is then we're going to set the output to equal zero comma one stored inside an array and again
26
00:02:00,540 --> 00:02:03,270
it will be returned at the end.
27
00:02:03,270 --> 00:02:11,250
Now finally if it's not one and it's not too well then we have to sum the last two values in the output
28
00:02:11,820 --> 00:02:20,310
so we can catch that final condition using an L statement and inside here we're going to set the output
29
00:02:20,640 --> 00:02:27,930
to equal to zero comma 1 to begin because this is the start of our sequence and then we're going to
30
00:02:27,930 --> 00:02:36,900
sum the last two values and we can do that by reaching into the outputs and getting hold of the first
31
00:02:36,900 --> 00:02:37,350
value.
32
00:02:37,350 --> 00:02:47,580
So output at position 0 which is this one and then we add that to the output at position 1.
33
00:02:47,580 --> 00:02:56,430
So now we're basically adding this first item to the second item and this should equal 1 and that should
34
00:02:56,430 --> 00:03:00,270
be somehow added to the end of this existing array.
35
00:03:01,020 --> 00:03:07,650
And if you remember from previous lessons on arrays we can do that by saying output dot push and we
36
00:03:07,650 --> 00:03:16,380
can wrap this calculation inside of parentheses and we will end up adding this solution to the existing
37
00:03:16,380 --> 00:03:17,180
array.
38
00:03:17,280 --> 00:03:26,910
And at this point the output should now look like zero comma one comma 1 and it will get returned.
39
00:03:26,910 --> 00:03:32,570
Now we have to check whether if N equals the number of items in the output.
40
00:03:32,790 --> 00:03:41,290
So we could do this using another if statement and we could say F and triple equals output length.
41
00:03:41,370 --> 00:03:52,290
Then we can go ahead and return the output but otherwise we have to continue to add the last two items
42
00:03:52,290 --> 00:03:52,850
together.
43
00:03:53,100 --> 00:04:02,310
So how could we change our code here so that instead of manually saying it's the item from the output
44
00:04:02,310 --> 00:04:10,050
array at position 0 this one plus the item at position 1 This one.
45
00:04:10,050 --> 00:04:17,110
How can we say instead that we want to get the last item plus the second from last item.
46
00:04:17,190 --> 00:04:20,020
Well we could use the length.
47
00:04:20,070 --> 00:04:24,320
So in this case output length is going to be equal to 2.
48
00:04:24,360 --> 00:04:35,270
So if we wanted this to be one then we could say output dot length minus one and then we have our second
49
00:04:35,330 --> 00:04:41,320
from the end which is going to be output length minus two.
50
00:04:41,390 --> 00:04:43,950
So this still works exactly the same.
51
00:04:44,060 --> 00: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
52
00:04:52,610 --> 00:04:59,470
output length at this point is going to be equal to four so four minus one is going to be three.
53
00:04:59,810 --> 00:05:08,810
So this becomes three and if we look at the item at position three in our Rea it's zero one two three.
54
00:05:08,840 --> 00:05:18,230
So it's this last item and minus two makes it 2 and 0 1 2 becomes the second from last item.
55
00:05:18,230 --> 00:05:20,730
So we're adding one plus two here.
56
00:05:20,810 --> 00:05:28,280
So this line of code now makes it dynamic and we could now use it inside our L statement.
57
00:05:28,280 --> 00:05:32,030
But notice how these two lines of code are now repeating.
58
00:05:32,600 --> 00:05:39,200
And also when we get to the end of the L statement we have no way of going back to the beginning to
59
00:05:39,200 --> 00:05:46,820
check if the n is equal to output length again like what is required here given how much this looks
60
00:05:46,820 --> 00:05:48,210
like a circle.
61
00:05:48,350 --> 00:05:51,100
It should remind you that we need to use a loop.
62
00:05:51,590 --> 00:05:58,800
So instead of writing all of this we could just simply create a loop and the type of loop that I'm going
63
00:05:58,800 --> 00:06:01,920
to create in this case is a for loop.
64
00:06:02,030 --> 00:06:09,410
So I'm gonna say let's create a variable inside the for loop that set equal to so the existing number
65
00:06:09,410 --> 00:06:19,940
of items in our output and then we're going to use a semicolon and say that while i is less than n the
66
00:06:19,940 --> 00:06:24,380
total number of items we need in our output continue to increase.
67
00:06:24,380 --> 00:06:34,790
I buy one and every single time what you want to do is to do this to get the last item from the outputs
68
00:06:35,000 --> 00:06:38,570
to get the second from the last item from the outputs.
69
00:06:38,570 --> 00:06:45,650
Add them together and then push it onto our array and finally return the output.
70
00:06:46,160 --> 00:06:52,970
So now let's take off over Nazi generator and then paste it into our wrap lit playground.
71
00:06:52,970 --> 00:06:59,960
And now let's go ahead and try to run our code by calling the Fibonacci generator and let's just start
72
00:06:59,960 --> 00:07:01,190
off with something quite simple.
73
00:07:01,190 --> 00:07:04,320
Let's start with an equals 1.
74
00:07:04,340 --> 00:07:08,920
Now if I hit run in the output I get zero.
75
00:07:08,960 --> 00:07:21,320
If I change this to 2 in the output I get 0 1 and if I change this to 5 in the output I get a 5 item
76
00:07:21,500 --> 00:07:24,170
sequence in an array.
77
00:07:24,170 --> 00:07:30,410
So now that we've confirmed that our code works let's go ahead and click check solution to see if we
78
00:07:30,410 --> 00:07:31,420
got it right.
79
00:07:31,430 --> 00:07:31,820
Brilliant.
80
00:07:31,820 --> 00:07:34,430
So now we've passed this challenge.
81
00:07:34,430 --> 00:07:36,550
How did you get on with this challenge.
82
00:07:36,590 --> 00:07:44,000
Did you struggle with maybe using some of the array methods or did you remember to use a loop so that
83
00:07:44,000 --> 00:07:48,240
it goes around and around and does the same action repeatedly.
84
00:07:48,290 --> 00:07:52,000
Now remember that there's many many ways of solving this challenge.
85
00:07:52,010 --> 00:07:58,170
You could have used a while loop you could have done something fancy instead of using many ifs and else
86
00:07:58,180 --> 00:08:00,640
is there's a lot of ways.
87
00:08:00,710 --> 00:08:07,250
But as long as your output satisfies the criteria that we set out in the challenge then it doesn't matter
88
00:08:07,250 --> 00:08:09,010
which way you chose.
89
00:08:09,110 --> 00:08:15,920
In fact at this stage as long as your solution makes sense to you then that will be the perfect solution.
9116
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.