All language subtitles for 014 Fibonacci Solution.en

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French Download
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranî)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

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.