All language subtitles for 12- Exercise 3- FizzBuzz

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
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
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

This next

exercise I want you to work on, is a very popular interview question

called the fizzBuzz algorithm. So we have this

function called fizz buzz, we give it an input and it

returns a string. Let me show you how that works, so

let's declare a constant called output. Here we call

fizzBuzz and pass an input like

3. Then, log the output

on the console. Let's see what we get. We get

fizz. So if the number that we're passing is divisible

by 3, we get fizz. So let me show you here.

If the number is divisible by 3 we get fizz.

Now if the number that we pass is divisible by 5,

we get buzz. So,

divisible by 5 we get buzz.

Now if the number that we're passing is divisible by 3 and 5,

like 15, we get fizz buzz.

So, divisible by

both 3 and 5, we get fizzBuzz.

Now if the number is not divisible by either 3 or

5, we're going to get the same number, so if you pass

let's say 7, we get 7, so

not divisible by 3 or 5, we get

the same input. Now if we don't pass a number here, let's say

we pass a string, we should get this message,

not a number. The same is true if you pass

a boolean, or anything that is not a number. So

if the input is not a number, that is the message that

we should display on the console. So I want you to pause

the video. Spend 10-15 minutes on this exercise,

and when you're done come back and continue watching.

Alright, let's see how we can implement

this fizzBuzz function. So we get the input

first we want to see if this is a number or not. Because if it's not a number

then we don't care about dividing that number by 3 or 5.

So, for that we use the type of operator. So,

typeof input is not number,

we want to return this message, not

a number. So that's the first if statement

in this function. Now if we get to this point then that means we have a number

we want to see if this number is divisible by 3,

5, both of them, or none of them. We can write

the if statement like this. If input

module is 3, equals 3, that means

this number is divisible by 3, so

we return fizz. Similarly, we

have another if statement. So if

input module is 5, equals

0, we're going to return buzz,

next we want to see if this number is divisible

by both 3 and 5. So, if

input is divisible by 3,

and input is divisible by

5 as well. Now here we're dealing with a complex expression,

to make this code more readable, I would like to put

each expression in parenthesis, like this. So

here we have one expression, we have the logical and

and the second expression. We can select this expression and type

the opening parenthesis, and this automatically adds the

closing parenthesis. That's a quick tip for you. So if this number is

divisible by both 3 and 5, you want to

return fizzBuzz, and

if we get to this point, that means that number is not divisible

by either 3 or 5. So we simply return the same

input. Now let's test this function. So initially

we have passed false, we should get not a number on the console.

Save the changes, we get not a number, beautiful, what happens if

we pass 3, we get 3, what happens if we pass

5. We get buzz. So far so good,

what if we pass 15. 15 is divisible by both

3 and 5, so we should get fizzBuzz.

Save the changes, wait a sec, what happened here? We got fizz

when we expected fizzBuzz. The reason for that is because

of how we have ordered our if statements. So in this case

15 is divisible by 3, so this if statement is executed

and here we immediately return fizz. To solve this

problem, we need to move this if statement

on the top. So I've selected these two lines, now to move this

code up, we can simply press the alt key and the up

arrow. Simple as that.

Okay, so if the number is divisible by

both 3 and 5, we need it to return fizzBuzz.

Now, let's save the changes,

Okay, the problem is solved, what if we pass a number that is not

divisible by 3 or 5. Let's say 7.

Now we get the same input beautiful, so this is the fizzBuzz algorithm,

that you see in a lot of programming interviews. Now I want to show you

something. In JavaScript we have this special value called

not a number. So here I'm going to replace this string

with not a number. Whenever you're dealing with

some mathematical calculation, if the result is not a number,

this value is returned. So we can use the same value

instead of returning a string like not a number. Now, this was not part of the

exercise, it's something that I'm teaching you now. So let's save the changes,

now, instead of passing a number, let's pass false.

Now we get not a number. Let's take a look

at the type of this value. So type of not a number,

weirdly enough, the type of this value is a number,

the value is something specific that is not a valid

mathematical number.

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.