Now, in the next lesson, I've got another coding challenge for you. And the idea of the coding challenge
is for you to be able to replicate the Fibonacci sequence.
So the Fibonacci sequence is very simple.
Essentially, let's say that the first two digits start with 0 and 1, every subsequent number
in this sequence is created by adding the two previous numbers.
So, for example, 3 is from 1 + 2,
5 comes from 2 + 3, 31 comes from 13 + 21.
So it's a really, really simple sequence.
Now, your goal is to create a function where you can call it by simply writing fibonacciGenerator(n)
and then you put n inside and n is going to be the number of items in the sequence that
you want to create.
So, for example, if you want to get the first three items in the Fibonacci sequence, then you should
be able to call fibonacciGenerator(3), pass in the number 3 and get [0, 1, 1] as an array as the output.
Now there's a couple of things to note before you start tackling the challenge.
First is that the solution checker is going to expect an array as the output with square brackets and
commas that separate each number.
The next thing is do not change any of the existing code.
The solution checker is going to look for this function fibonacciGenerator() and pass in a number.
So if you change any of the existing code, it might not work and it might think that you've written
the code wrong instead.
Next, you do not need any alerts or prompts and the result should in fact be returned from this function
as an output.
Next, the first two numbers in the sequence must be 0
and 1. There are many versions of the Fibonacci sequence,
some start from 1, some start from 0,
but in our version, it's going to start from 0 and then the next one is going to be 1.
So whenever you're generating any sort of sequence, the first two numbers must be 0 and 1.
Now, the final thing to say is that if you're going to use a for loop in your code, make sure that
you write it like this.
So you create i as an explicit variable,
so you say var i = 0 as the first part of the for loop rather than simply writing i = 0.
This is just because the version of JavaScript that's being used to check the code has this specific
requirement.
Now, you don't have to use a for loop, you could use a while loop.
You can use anything that you're comfortable with.
Now finally, I've got this Repl.it playground here and if you click on it, you'll get taken to a sandbox
on Repl.it where you can play around with your code and see if it actually does what it's expected to
do.
Now, if all goes well and I'm scrolling down to hide my solution code here, but if you manage to write
the code correctly, you should be able to create a variable called output, which is going to store the
output that's returned from your function, fibonacciGenerator().
And if we pass in a number, let's say 5, and we run our code, then it should give us an array containing
five items.
And the five items will be the first five numbers in the sequence.
So as we said, it must start from 0 and then 1, and then the next one comes from 0 + 1,
the next one comes from 1 + 1, etcetera, etcetera.
And this should work even when this number is 1, or if it's 2, or if it's some crazy large number
like 245.
Now, the hardest part of this challenge is nailing down the logic.
And you really have to think carefully to yourself
what do all of those rules mean?
And one of the best ways of untangling the logic before you start writing code is to just create a flowchart.
So you can easily create a flowchart by going to a website like draw.io and you can start mapping out
what the logic has to look like.
And then once you've got the flowchart, then you can use that to create your code.
Now, I've created a flowchart for you.
If you want to have a go at thinking about the logic and solving the challenge yourself, then pause
the video now and continue to the next lesson and start the challenge.
But if you want a few hints, I'll walk you through the logic in my flowchart.
All right, so if you're still here, let's walk through this flowchart.
Let's say that we're going to call the function fibonacciGenerator and pass in 5 as the value of n.
So we start from a flowchart and we check first, is n 1?
If it's yes, then we're going to output just an array with the first number, which is 0.
If it's no, then we're going to check further. Is n 2? In which case will give the output as an array
with 0 and 1.
These two are the ones that we can't calculate.
They're the first two items of the array and they're just predefined.
Now if that question also gives us a no, because in our case, n is actually equal to 5,
well, in this case we have to sum the last two values
so 0 + 1 is going to be 1.
So now we've got an array that looks like this.
Now, at this stage, we have to ask ourselves, does n equal the number of items in the output?
So the number of items in our output is one, two, three, while n equals 5,
so, no.
So we go back to over here and we sum the last two values again.
So in this case, the last two values are 1 and 1
so we now get 2. And then we continue this circle of logic until we get to the point where we have
the same number of items in our array, 5, as the number n 5. Well at this point, then this output is going
to be sent out and returned from the function.
So this is the logic of the code that we need to write.
So now have a think about this and I've got a link to this flowchart in the course resources if
you need to refer to it. But have a good think about it and then head over to the next lesson and try
to tackle the challenge.