Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
All right, I hope you gave this challenge a good go before coming over to the solution. And it's because
you only really learn when you try and fail and go through challenges like this.
So if you haven't given enough time, stop.
Think about it.
Wait. Come back to it.
And only if you get really stuck, then come over here and check through the solution with me.
Now, I'm going to be working off the flowchart that I showed you previous to the challenge.
So we're going to be converting all of this logic into code.
So to begin, we start out with an output that's equal to an empty array.
So let's write that in our code.
So let's create some sort of variable called output and we can set that to an empty array with nothing
inside.
Now, the next step is we have to check whether if n is equal to 1. And if so, we're going to give
the output as 0.
So to do that, we'll use an if statement to check if this n that's being passed in is equal to 1.
And if so, then we're going to say that output is going to be equal to just [0].
So this is the first item in the sequence and then we can go ahead and return the output.
Now, the next step in our flowchart is, well, if it wasn't equal to 1, then is it equal to 2?
And if so, then the output should be [0, 1].
So we can represent this logic by using an else if. Remember that else if only gets checked if the first
one was false.
So now we can check
well, if it's not equal to 1, well, is it equal to 2?
So if it is, then we're going to set the output to equal [0, 1] stored inside an array, and
again, it will be returned at the end.
Now, finally, if it's not 1 and it's not 2, well, then we have to sum the last two values in
the output. So we can catch that final condition using an else statement.
And inside here, we're going to set the output to equal to [0, 1] to begin, because this is
the start of our sequence,
and then we're going to sum the last two values.
And we can do that by reaching into the outputs and getting hold of the first value,
so output [0], which is this one. And then we add that to the output[1].
So now we're basically adding this first item to the second item and this should equal 1, and that
should be somehow added to the end of this existing array.
And if you remember from previous lessons on arrays, we can do that by saying output.push. And
we can wrap this calculation inside parentheses and we will end up adding this solution to the existing
array.
And at this point, the output should now look like [0, 1, 1] and it will get returned.
Now we have to check whether if n equals the number of items in the output.
So we could do this using another if statement, and we could say if n triple equals output length, then
we can go ahead and return the output.
But otherwise, we have to continue to add the last two items together.
So how could we change our code here so that instead of manually saying it's the item from the
output array position 0, this one, plus the item at position 1, this one.
How can we say instead that we want to get the last item plus the second from the last item?
Well, we could use the length.
So in this case, output.length is going to be equal to 2.
So if we wanted this to be 1, then we could say output.length
- 1, and then we have our second from the end, which is going to be output.length - 2.
So this still works exactly the same
but now this line of code can work no matter the size of our array. Because even if it was this long,
output.length at this point is going to be equal to 4.
So 4 - 1 is going to be 3
so this becomes 3.
And if we look at the item at position 3 in our array, it's 0, 1, 2, 3
so it's this last item. And - 2 makes it 2, and 0, 1, 2 becomes the second from the last item.
So we're adding 1 + 2 here.
So this line of code now makes it dynamic and we could now use it inside our else statement.
But notice how these two lines of code are now repeating.
And also when we get to the end of the else statement, we have no way of going back to the beginning to
check if the n is equal to output.length
again, like what is required here.
Given how much this looks like a circle, it should remind you that we need to use a loop.
So instead of writing all of this, we could just simply create a loop.
And the type of loop that I'm going to create in this case is a for loop.
So I'm going to say let's create a variable inside the for loop that's set to equal 2, so the existing
number of items in our output.
And then we're going to use a semicolon and say that while i is less than n, the total number of items
we need in our output, continue to increase i by 1. And every single time
what you want to do is to do this to get the last item from the outputs, to get the second from the
last item from the output, add them together and then push it onto our array. And finally return the
output.
So now let's take our fibonacciGenerator and then paste it into our Repl.it playground.
And now let's go ahead and try to run our code by calling the fibonacciGenerator.
And let's just start off with something quite simple.
Let's start with n = 1.
Now, if I hit run in the output, I get [0].
If I change this to 2 in the output, I get [0, 1].
And if I change this to 5 in the output, I get a five-item sequence in an array.
So now that we've confirmed that our code works, let's go ahead and click check solution to see if
we got it right.
Brilliant.
So now we've passed this challenge.
How did you get on with this challenge?
Did you struggle with maybe using some of the array methods or did you remember to use a loop so that
it goes around and around and does the same action repeatedly?
Now, remember that there's many, many ways of solving this challenge.
You could have used a while loop,
you could have done something fancy instead of using many ifs and else ifs.
There's a lot of ways.
But as long as your output satisfies the criteria that we set out in the challenge, then it doesn't
matter which way you chose.
In fact, at this stage, as long as your solution makes sense to you, then that will be the perfect
solution.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.