It's time for exercise eight. This is the first of two exercises for functions. Let's take a look at what the assignment is. So I'd like you to work with the Fibonacci sequence again, since that's familiar, won't force you to learn additional concepts and try to focus your learning on the parts of JavaScript that we'll be talking about. So for this exercise, you need to create a function that will return a Fibonacci sequence, the length of the sequence sequence will be passed in to the function. If we want a Fibonacci sequence of 10 numbers 10 would be passed in.
And then the Fibonacci sequence will be returned as an array. So a bit different than what we did previously will allow you to apply some additional things you've learned with JavaScript When you're done with this exercise, test it with several numbers to make sure it's working. I'm just a reminder if you've forgotten what a Fibonacci sequence is, is simply that each subsequent number is the sum of the previous two numbers. So there's an example shown there. Alright, go ahead and pause the video at this time. And then when you're ready to continue, restart it.
Alright, hopefully you were able to solve that. Now just be aware, there are many ways to go ahead and solve this particular problem. So I'm going to show one method which I used. But if you're able to solve the problem fine. It doesn't mean it has to look exactly like mine. But you can learn something from different ways to solve the same type of problem.
So I'm going to declare a variable for My function, so I'm going to do a function expression passing in a length argument. So I'll use ln for that variable, and then I'll create my curly braces. put a semicolon at the end of that expression. All right, I'm going to create two variables. Since we need to return an array, I'm going to use an array as the primary means for storing each new number and the sequence as a whole. So let's call that a CQ.
I'm going to set that to the initial part of the sequence. Then one more variable, and this will be the new known that we need. Create. So we'll create a new num for the sequence and then add it to the sequence. All right. Now, in the first time we did this particular exercise, we use the while loop, or I use the while loop in the the solution that I showed.
However, this particular requirement seems to lend itself very well to a four loop, because we're passing in the length of that sequence, how many numbers are going to be in that sequence? And so that lends itself very well to a for loop. So for I mean, you use lat to declare the variable in the for loop. Now I'm going to set it equal to two. Now, why am I doing that? Well, I want I to track within the array where the next number goes, and right now I have the first part of that sequence.
Set up. So in index zero index one, so the next one would be index two. So that's how I'm going to start the for loop. We want this for loop to run while i is less than or equal to the length that was passed in. Because that would mean we would then have that number within our sequence that number of elements in our sequence. And then finally, we want to increment i curly braces for the code block.
And so I'm going to add a new number to my array. So SC Q, and then I as the index for that new number. We're going to set that equal to and now I'm going to sum the previous to numbers and I'll just use the array for that as well. CQ minus one. So that's the previous position, plus CQI minus two. That goes back one more position, it will sum those two, and then it will insert the results into the next position in the array.
And then we will iterate one to the next number. And then it does the same thing all over again until we've completed. Now that we've got our sequence, I'm going to simply return a CQ, which is our array. All right, now we need to test this with a couple of numbers at least. So I'm just going to log out the results. I'm going to call fibs C. I'm going to pass in a 10 for the first example.
And then it will return the results in an array. So I'm going to convert that array to a string whoops. So that will log out to the console. Then I'll do one more example. And I'll do 20 for this one. All right, let's go ahead and check that out, save it.
Copy the file path, jump out here. Open the console. And we have a Fibonacci sequence. But notice something looks like I made a mistake in what I was creating. If we count these numbers 1-234-567-8910 11 that's more than I should have. I should have only had 10 there.
So in my thinking about the for loop, I was incorrect. And that's sometimes how it goes. When you're in Solving particular problems. And so that's a pretty simple thing to change, I simply need to remove the equal sign here and make it less than length. And really, that's what it should have been, since length is the number of items in the array. And I'm using position so I'm starting with zero.
And so that makes sense that I would have to do less than that. So now let's save that. And let's display it one more time. Now we have 1-234-567-8910. Great, and I'm not going to count that one and that one's probably 20 as well. But you can see how quickly the numbers in a Fibonacci sequence get pretty large.
All right, let's move on to the next exercise.