All language subtitles for 119 Functions Part 2_ Parameters and Arguments.en_US

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic Download
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
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

Now in the last lesson we saw the most basic type of Javascript functions and that is a way of allowing

us to package a number of instructions into a block of code which we can execute at once by using the

name of the function.

Now if any of that was confusing then I'd recommend reviewing the last lesson and playing around with

the basic function yourself either in the Chrome Developer Tools or through using Karel the robot,

and just making sure that you fully understand what's going on before you proceed.

Now, when I was little my parents used to love buying these Neapolitan ice creams, which come in three

flavors, right?

Vanilla, strawberry and chocolate.

And I always thought it was really indecisive of them, instead of just picking your favorite flavor,

right?

But just like ice cream comes in three classic flavors, functions also come in three classic flavors.

And what we saw just now was the vanilla version,

the simplest form of the Javascript function.

So let's take a look at the next flavor of functions and going back to our little house robot scenario.

Now what if I have the ability to not only say getMilk, which executes all of the commands inside the

getMilk function, but what if I could say getMilk,

and inside the parentheses, I specify the number of bottles of milk that I want the robot to get?

Well, in order to do this, we have to modify our simple function a little bit, and we have to allow it

to accept inputs, whereas previously we had nothing between the parentheses because that function didn't

take any inputs.

In this case we're putting in the name of the input that is going to go into this function and allows

it to be used inside the series of instructions.

So, in this case, when we call getMilk, it's still going to execute all of these alerts to the robot to

command it to do something.

But in the middle, when we get to the buy milk section, it's going to tell it to buy a specific number

of bottles of milk based on the input that the function gets when it gets called.

So, when we create the function we're still using the function keyword,

we’re still giving it the same name called getMilk,

but in this case in between the parentheses we've got the name of the input,

and in this case it's called bottles.

Now that bottles is used much like a variable to contain the input, and we can now use it inside the

function to calculate something or to do something with it.

In this case we're calculating the cost, say if one bottle milk costs 1.5 dollars, then depending

on the number of bottles of milk the master wanted, then we would have to calculate the amount of money

we have to give to the shopkeeper, right? Now, when you call this function, it's similar to what we did

previously, but this time, instead of having empty parentheses, we're giving the input of 2.

And this will get bound to that name, bottles, inside the function.

So if we call this function using getMilk(2), then 2 goes into bottles.

So now we have a variable called bottles that's equal to two and then we can calculate cost which equals

to 2 times 1.5 which equals 3.

So the cost is $3.

And this is the second flavor of functions which is kind of like the chocolate flavor and a little bit

better than vanilla.

And now when we say getMilk(2), our robot knows how many bottles of milk to get.

It’ll also be able to use that number of bottles to calculate how much money it needs to spend at the

store, and how much change it should get back for its master.

So we can use the input inside the block of code.

So, if we take a look at this inside our Chrome Developer Tools, if we modify our getMilk function to

take an input, say called bottles,

now we're able to use this variable,

bottles, inside the curly braces of this function.

So, for example, instead of simply console logging buy milk, I can now use that variable, bottles, inside

my code.

So now I want the console log to read, buy the number of bottles plus the string ‘bottles of milk’.

So now, when we call this function, we have to specify an input.

So let's say buy 12,

right?

So let's put 12 in here.

Now, once I hit run, you'll see that bottles now takes on the value of 12, and it gets placed inside this

console log statement.

So now, if I hit command K, or control K on Windows, to clear the console, and I run the code inside my Snippets,

then you'll see that, in addition to carrying out all the other commands,

it also says ‘buy 12 bottles of milk’, and that 12 comes from the input that I used to call this function,

getMilk.

Now this is a really neat way of modifying your function so that it does more than simply repeating

a bunch of code that you packaged together.

So as a challenge I want you to change this code here to take money instead of bottles.

And I want you to use it to calculate the number of bottles the robot is able to buy.

So, for example, if you gave your robot $10 and a bottle of milk costs $1.5, you should be able to modify

your function to calculate how many bottles of milk the robot is able to buy based on the amount of

money that you gave the robot.

So you should be able to say something like getMilk, maybe $5, and if one bottle of milk is say

$1.5, then that should mean that your robot should be able to buy 5 divide by 1.5 which equals

3.33, so it won't be able to buy a 0.3 bottle.

Nobody's going to let it slash a bottle of milk into a third, but it will be able to buy 3 bottles

of milk back.

So see if you can modify this function in order to take an amount of money as an input and be able to

change the console log to state how many bottles of milk it's going to buy based on how much money it

got.

So if you haven’t followed the function up till now, you can download this code here in the resources

section of this lesson and you can use it to complete the challenge.

So pause the video now and give it a go.

All right.

So this time instead of taking bottles as an input we're going to take money as the input and this is

just a name that we're going to assign to whatever number comes in through here.

So we're going to keep this function call the same.

So we're calling the function getMilk, giving the input a value of 5.

Now 5 gets bound to this word money and we can now use it inside the function.

So, for example, we can say something like var numberOfBottles

= money / 1.5, which we said in this scenario is the cost per bottle of milk, and then we’re

going to say, console.log “buy “ numberOfBottles “of milk”.

But right now, if we clean the console and run our code, you'll see that we're getting 3.3 recurring

bottles of milk, which is not what we want,

right?

We want it to only say 3.

So how can we get it to do that?

Well, this might have required a little bit of googling, but essentially what we're trying to do is,

how do we round down Javascript? So, we can see that the first link that we get is from the W3 Schools,

and you can see that it’s pointing us towards something called the Javascript floor method, where you

can write Math.floor,

and inside the parentheses we put the number that we want to round down.

So let's give this a go over here.

What if we said numberOfBottles = Math.floor,

and then inside these round parentheses we put in the number that we want to round down?

Now, it's important that we're rounding down because even if it was 4.99 bottles, we still

can't buy 0.99 bottles of milk,

right?

So we have to round it down.

So now, if we run our code with this floor function here, then you can see that our code now tells the

robot to buy 3 bottles of milk, which is exactly what we wanted.

So very often, when you try to do something with code, anything that you’re trying to achieve is usually

just a quick google away.

So I think that challenge was interesting.

And in the next lesson we're going to look at an even more complex form of functions, which is functions

that can give an output back in addition to taking an input.

So for all of that and more, I'll see you on the next lesson.

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