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