Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:00,600 --> 00:00:04,830
Now in the next lesson I've got a nother coding challenge for you.
2
00:00:04,830 --> 00:00:10,860
And the idea of the coding challenge is for you to be able to replicate the Fibonacci sequence so the
3
00:00:10,860 --> 00:00:13,480
Fibonacci sequence is very simple.
4
00:00:13,650 --> 00:00:19,860
Essentially let's say that the first two digits start with zero and one every subsequent number in this
5
00:00:19,860 --> 00:00:23,910
sequence is created by adding the two previous numbers.
6
00:00:23,910 --> 00:00:27,230
So for example three is from one plus two.
7
00:00:27,390 --> 00:00:29,790
Five comes from two plus three.
8
00:00:29,880 --> 00:00:32,180
31 comes from 13 plus 21.
9
00:00:32,610 --> 00:00:35,750
So it's a really really simple sequence.
10
00:00:35,820 --> 00:00:44,490
Now your goal is to create a function where you can call it by simply writing Fibonacci generator parentheses
11
00:00:44,760 --> 00:00:52,320
and then you put an inside and n is going to be the number of items in the sequence that you want to
12
00:00:52,320 --> 00:00:53,310
create.
13
00:00:53,310 --> 00:00:59,340
So for example if you want to get the first three items in the seven R2 sequence then you should be
14
00:00:59,340 --> 00:01:07,760
at to call Fibonacci generator pass in the number three and get 0 1 1 as an array as the output.
15
00:01:07,770 --> 00:01:11,640
Now there's a couple of things to note before you start tackling the challenge.
16
00:01:11,640 --> 00:01:18,330
First is that the solution checker is going to expect an array as the output with square brackets and
17
00:01:18,330 --> 00:01:20,930
commas that separate each number.
18
00:01:20,970 --> 00:01:24,560
The next thing is do not change any of the existing code.
19
00:01:24,570 --> 00:01:30,330
The solution checker is going to look for this function Fibonacci generator and pass in a number.
20
00:01:30,360 --> 00:01:35,430
So if you change any of the existing code it might not work and it might think that you've written the
21
00:01:35,430 --> 00:01:37,680
code wrong instead.
22
00:01:37,680 --> 00:01:44,550
Next you do not need any alerts or prompt and the result should in fact be returned from this function
23
00:01:45,000 --> 00:01:46,750
as an output next.
24
00:01:46,860 --> 00:01:51,540
The first two numbers in the sequence must be 0 and 1.
25
00:01:51,540 --> 00:01:54,520
There are many versions of the Fibonacci sequence.
26
00:01:54,600 --> 00:02:01,050
Some start from one some start from zero but in our version it's going to start from zero and then the
27
00:02:01,050 --> 00:02:03,050
next one is going to be 1.
28
00:02:03,060 --> 00:02:09,320
So whenever you're generating any sort of sequence the first two numbers must be 0 and 1.
29
00:02:09,360 --> 00:02:15,570
Now the final thing to say is that if you're going to use a for loop in your code make sure that you
30
00:02:15,570 --> 00:02:17,230
write it like this.
31
00:02:17,250 --> 00:02:25,110
So you create AI as an explicit variables use a var i equals zero as the first part of the for loop
32
00:02:25,530 --> 00:02:28,530
rather than simply writing I equals zero.
33
00:02:28,530 --> 00:02:34,560
This is just because the version of JavaScript that's being used to check the code has this specific
34
00:02:34,560 --> 00:02:35,880
requirement.
35
00:02:36,060 --> 00:02:37,920
Now you don't have to use a for loop.
36
00:02:37,920 --> 00:02:42,000
You could use a while loop you can use anything that you're comfortable with.
37
00:02:42,060 --> 00:02:45,460
Now finally I've got this replica playground here.
38
00:02:45,630 --> 00:02:51,900
And if you click on it you'll get taken to a sandbox on Rappler where you can play around with your
39
00:02:51,900 --> 00:02:56,790
code and see if it actually does what it's expected to do.
40
00:02:56,790 --> 00:03:01,350
Now if all goes well and I'm scrolling down to hide my solution code here.
41
00:03:01,380 --> 00:03:06,960
But if you manage to write the code correctly you should be able to create a variable code output which
42
00:03:06,960 --> 00:03:12,690
is going to store the output that's returned from your function Fibonacci generator.
43
00:03:12,810 --> 00:03:20,220
And if we pass in a number let's say 5 and we run our code then it should give us an array containing
44
00:03:20,220 --> 00:03:26,280
5 items and the 5 items will be the first 5 numbers in the sequence.
45
00:03:26,280 --> 00:03:31,950
So as we said it must start from zero and then 1 and then the next one comes from 0 plus 1.
46
00:03:32,010 --> 00:03:41,070
The next one comes from one plus one etc. etc. and this should work even when this number is 1 or if
47
00:03:41,070 --> 00:03:45,300
it's 2 or if it's some crazy large number.
48
00:03:45,300 --> 00:03:48,090
Like 245.
49
00:03:48,090 --> 00:03:55,020
Now the hardest part of this challenge is nailing down the logic and you really have to think carefully
50
00:03:55,020 --> 00:03:58,160
to yourself what do all of those rules mean.
51
00:03:58,320 --> 00:04:05,820
And one of the best ways of untangling the logic before you start writing code is to just create a flowchart
52
00:04:06,240 --> 00:04:13,260
so you can easily create a flowchart by going to a website like draw IO and you can start mapping out
53
00:04:13,500 --> 00:04:15,730
what the logic has to look like.
54
00:04:15,810 --> 00:04:20,300
And then once you've got the flowchart then you can use that to create your code.
55
00:04:20,310 --> 00:04:27,840
Now I've created a flowchart for you if you want to have a go at thinking about the logic and solving
56
00:04:27,840 --> 00:04:34,940
the challenge yourself then pause the video now and continue to the next lesson and start the challenge.
57
00:04:35,040 --> 00:04:42,420
But if you want a few hints I'll walk you through the logic in my flowchart All right.
58
00:04:42,450 --> 00:04:43,530
So if you're still here.
59
00:04:43,530 --> 00:04:45,060
Let's walk through this flowchart.
60
00:04:45,420 --> 00:04:51,330
Let's say that we're going to call the function Fibonacci generator and parse in five as the value of
61
00:04:51,380 --> 00:04:52,010
n..
62
00:04:52,230 --> 00:04:56,790
So we start from our flowchart and we check first is n 1.
63
00:04:57,330 --> 00:05:03,060
If it's yes then we're going to output just an array with the first number which is zero.
64
00:05:03,810 --> 00:05:10,260
If it's no then we're going to check further is and 2 in which case we'll give the output as an array
65
00:05:10,260 --> 00:05:12,320
with 0 and 1.
66
00:05:12,360 --> 00:05:15,260
These two are the ones that we can't calculate.
67
00:05:15,300 --> 00:05:19,520
They're the first two items of the array and they're just predefined.
68
00:05:19,530 --> 00:05:26,380
Now if that question also gives us a no because in our case N is actually equal to 5.
69
00:05:26,430 --> 00:05:29,490
Well in this case we have to sum the last two values.
70
00:05:29,490 --> 00:05:33,210
So zero plus one is going to be one.
71
00:05:33,570 --> 00:05:37,080
So now we've got an array that looks like this.
72
00:05:37,290 --> 00:05:42,690
Now at this stage we have to ask ourselves does an equal the number of items in the output.
73
00:05:42,690 --> 00:05:49,160
So the number of items in our output is 1 2 3 while N equals 5.
74
00:05:49,170 --> 00:05:50,170
So no.
75
00:05:50,190 --> 00:05:54,510
So we go back to over here and we sum the last two values again.
76
00:05:54,510 --> 00:05:57,910
So in this case the last two values are 1 and 1.
77
00:05:57,960 --> 00:06:06,990
So we now get two and then we continue this circle of logic until we get to the point where we have
78
00:06:07,020 --> 00:06:16,080
the same number of items in our array five as the number and five will at this point then this output
79
00:06:16,260 --> 00:06:20,470
is going to be sent out and returned from the function.
80
00:06:20,490 --> 00:06:24,760
So this is the logic of the code that we need to write.
81
00:06:24,780 --> 00:06:30,540
So now I have a think about this and I've got a link to this flowchart in the course resources if you
82
00:06:30,540 --> 00:06:36,210
need to refer to it but have a good think about it and then head over to the next lesson and try to
83
00:06:36,210 --> 00:06:37,320
tackle the challenge.
8419
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.