WEBVTT 0 00:00.540 --> 00:06.660 Now, in the next lesson, I've got another coding challenge for you. And the idea of the coding challenge 1 00:06.660 --> 00:10.200 is for you to be able to replicate the Fibonacci sequence. 2 00:10.500 --> 00:13.190 So the Fibonacci sequence is very simple. 3 00:13.530 --> 00:19.530 Essentially, let's say that the first two digits start with 0 and 1, every subsequent number 4 00:19.530 --> 00:23.590 in this sequence is created by adding the two previous numbers. 5 00:23.850 --> 00:27.060 So, for example, 3 is from 1 + 2, 6 00:27.360 --> 00:32.180 5 comes from 2 + 3, 31 comes from 13 + 21. 7 00:32.580 --> 00:35.290 So it's a really, really simple sequence. 8 00:35.760 --> 00:42.910 Now, your goal is to create a function where you can call it by simply writing fibonacciGenerator 9 00:43.560 --> 00:51.900 () and then you put n inside and n is going to be the number of items in the sequence that 10 00:51.900 --> 00:52.830 you want to create. 11 00:53.250 --> 00:59.250 So, for example, if you want to get the first three items in the Fibonacci sequence, then you should 12 00:59.250 --> 01:07.410 be able to call fibonacciGenerator, pass in the number 3 and get 0, 1, 1 as an array as the output. 13 01:07.710 --> 01:11.160 Now there's a couple of things to note before you start tackling the challenge. 14 01:11.640 --> 01:18.360 First is that the solution checker is going to expect an array as the output with square brackets and 15 01:18.360 --> 01:20.250 commas that separate each number. 16 01:20.970 --> 01:24.270 The next thing is do not change any of the existing code. 17 01:24.540 --> 01:30.250 The solution checker is going to look for this function Fibonacci generator and pass in a number. 18 01:30.330 --> 01:35.370 So if you change any of the existing code, it might not work and it might think that you've written 19 01:35.370 --> 01:36.450 the code wrong instead. 20 01:37.650 --> 01:44.580 Next, you do not need any alerts or prompts and the result should in fact be returned from this function 21 01:44.880 --> 01:45.760 as an output. 22 01:46.080 --> 01:50.700 Next, the first two numbers in the sequence must be 0 23 01:50.700 --> 01:54.270 and 1. There are many versions of the Fibonacci sequence. 24 01:54.600 --> 01:57.240 Some start from 1, some start from 0. 25 01:57.510 --> 02:02.630 But in our version, it's going to start from 0 and then the next one is going to be 1. 26 02:03.030 --> 02:08.820 So whenever you're generating any sort of sequence, the first two numbers must be 0 and 1. 27 02:09.300 --> 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 28 02:15.390 --> 02:17.180 you write it like this. 29 02:17.220 --> 02:20.970 So you create i as an explicit variables, 30 02:20.980 --> 02:28.090 so you say var i = 0 as the first part of the for loop rather than simply writing i = 0. 31 02:28.500 --> 02:34.560 This is just because the version of JavaScript that's being used to check the code has this specific 32 02:34.560 --> 02:35.180 requirement. 33 02:36.000 --> 02:39.150 Now, you don't have to use a for loop, you could use a while loop. 34 02:39.270 --> 02:41.550 You can use anything that you're comfortable with. 35 02:42.030 --> 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 36 02:49.170 --> 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 37 02:55.950 --> 02:56.250 do. 38 02:56.760 --> 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 39 03:02.370 --> 03:08.700 the code correctly, you should be able to create a variable called output, which is going to store the 40 03:08.700 --> 03:12.310 output that's returned from your function fibonacciGenerator. 41 03:12.720 --> 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 42 03:20.250 --> 03:21.390 five items. 43 03:21.780 --> 03:25.910 And the five items will be the first five numbers in the sequence. 44 03:26.220 --> 03:31.770 So as we said, it must start from 0 and then 1, and then the next one comes from 0 + 1, 45 03:31.980 --> 03:35.190 the next one comes from 1 + 1, etc, etc. 46 03:35.580 --> 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 47 03:45.300 --> 03:47.070 like 245. 48 03:48.060 --> 03:52.140 Now, the hardest part of this challenge is nailing down the logic. 49 03:52.560 --> 03:55.560 And you really have to think carefully to yourself 50 03:55.830 --> 03:57.930 what do all of those rules mean? 51 03:58.230 --> 04:05.850 And one of the best ways of untangling the logic before you start writing code is to just create a flowchart. 52 04:06.180 --> 04:13.290 So you can easily create a flowchart by going to a website like draw.io and you can start mapping out 53 04:13.440 --> 04:15.480 what the logic has to look like. 54 04:15.720 --> 04:19.620 And then once you've got the flowchart, then you can use that to create your code. 55 04:20.290 --> 04:23.070 Now, I've created a flowchart for you. 56 04:23.580 --> 04:29.970 If you want to have a go at thinking about the logic and solving the challenge yourself, then pause 57 04:29.970 --> 04:34.560 the video now and continue to the next lesson and start the challenge. 58 04:34.980 --> 04:40.200 But if you want a few hints, I'll walk you through the logic in my flowchart. 59 04:41.910 --> 04:45.100 All right, so if you're still here, let's walk through this flowchart. 60 04:45.390 --> 04:51.360 Let's say that we're going to call the function fibonacciGenerator and pass in 5 as the value of n. 61 04:52.200 --> 04:56.760 So we start from a flowchart and we check first, is n 1? 62 04:57.240 --> 05:03.020 If it's yes, then we're going to output just an array with the first number, which is 0. 63 05:03.750 --> 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 64 05:10.260 --> 05:11.820 with 0 and 1. 65 05:12.330 --> 05:15.080 These two are the ones that we can't calculate. 66 05:15.240 --> 05:19.350 They're the first two items of the array and they're just predefined. 67 05:19.500 --> 05:25.980 Now if that question also gives us a no, because in our case, n is actually equal to 5, 68 05:26.400 --> 05:29.460 well, in this case we have to sum the last two values 69 05:29.460 --> 05:33.160 so 0 + 1 is going to be 1. 70 05:33.540 --> 05:36.600 So now we've got an array that looks like this. 71 05:37.260 --> 05:42.200 Now, at this stage, we have to ask ourselves, does n equal the number of items in the output? 72 05:42.630 --> 05:49.100 So the number of items in our output is one, two, three, while n equals 5. 73 05:49.110 --> 05:50.040 So, no. 74 05:50.160 --> 05:54.310 So we go back to over here and we sum the last two values again. 75 05:54.450 --> 05:57.590 So in this case, the last two values are 1 and 1 76 05:57.930 --> 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 77 06:07.020 --> 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 78 06:16.650 --> 06:19.950 to be sent out and returned from the function. 79 06:20.460 --> 06:24.090 So this is the logic of the code that we need to write. 80 06:24.750 --> 06:30.480 So now have a think about this and I've got a link to this flowchart in the course resources if 81 06:30.480 --> 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 82 06:36.120 --> 06:37.320 to tackle the challenge.