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