All language subtitles for 28. Challenge BMI Calculator Solution

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

0 All right. So we know that the formula is weight in kilograms divided by height squared, and we have to 1

make this line of code work. 2

So let's first try it out in the calculator. 3

Let's say that we first try to square our height which is 1.8 meters. 4

So on a calculator we can simply write 1.8 times 1.8. 5

So 1.8 squared is equal to 3.24. 6

So now we can divide 65 by 3.24. 65 7

divide by 3.24 and we get 20.0617 something something something. 8

All right. So now that we know that this is how we would calculate it on a calculator, then we can simply 9

copy all of this starting code, and open up our Chrome Developer Tools, and paste it into our Snippets 10

tool. 11

So now, we're going to create our function below this line. 12

And the first step to creating any sort of function is the function keyword, 13

right? Then we're going to give our function a name, which we said has to be the name bmiCalculator, 14

so we're going to use that name. 15

Next step is some parentheses, and whether we're going to add some inputs in here. 16

So in this case we're saying that put the input weight first and then put the input height. 17

So our inputs are going to be weight and height. 18

Now we get to open up our curly braces and specify everything that this function is going to do. 19

So this function has to calculate the value of bmi. 20

Remember that the formula is the weight divided by height squared. 21

So bmi is going to equal to the weight input divided by the height input multiplied by itself. 22

And remember, in the previous lesson we talked about Javascrpit numbers, that in order to specify the 23

order of your calculations then you can use the parentheses to set precedence. 24

So by putting those two parentheses there then we say that calculate height times height first and then 25

divide weight by that value. 26

So now in order to get value bmi out of this function, we have to use the return keyword, and we can say 27

return bmi. 28

So now, if we take all of this code and we go back to our exercise here, we paste it in here, and now if 29

I hit Check solution, then you can see that it will pass. 30

Now there's a number of different ways of writing this function. 31

Some of you might have wondered, 32

I don't really want to multiply the height by height in order to get it squared. 33

Why can’t I just raise height to the power of 2? 34

How would I do that in Javascript? 35

Well, as with any query regarding programming, the solution is only a quick google away. 36

So if we search for ‘raise a number to a power’ and we add the keyword Javascript, then you can see that the 37

MDN docs come up with the Math.pow function, and it shows us how to use it. 38

It says that we can simply say Math. 39

pow, 40

and the first number is the base and the second number is the exponent. 41

So this is your squared which will be 2 or cubed which will be 3. 42

And this is how you would use it. 43

So let's try it out 44

in our case. Instead of writing height times height, 45

what if we did Math. 46

pow, set of parentheses, and then we said height, 2, so height to the power of 2? 47

Now, if we check our solution, you can see that our function is still working exactly as we expect it 48

to, 49

but it's now perhaps a little bit more elegant. 50

Now, if you run this code as it is right now, for example we're calculating the BMI from 65 kilograms 51

and 1.8, and then we're logging that BMI into the console, 52

then you can see that we're getting a really long number, 53

20.0672839 etc.. 54

Now usually with BMI we're working with whole numbers. 55

So how can we turn this long number with loads of decimal places to simply a whole number? 56

Well again Google is your friend. 57

And if you search for ‘Javascript round a number to the nearest whole number’ or something around there, then 58

the first result that you get is from W3 Schools, and it tells you about the round method, which you can 59

use by saying Math.round, and is similar to the floor method, which simply rounded down your decimal 60

place. 61

But Math.round is more mathematically correct. 62

So if it's 2.5 it will round it to 3, and if it's 2.4 63

it’ll round it to 2. 64

So let's try using this in our solution. 65

So if, instead of returning bmi, we returned Math.round, and inside the parentheses we enclosed our 66

bmi, as a number with decimal places. 67

Now if we run our code you can see that we only get 20 back which is exactly what we wanted. 68

And if you change our solution to this updated one inside our testing suite, then you can see that our solution 69

also passes. 70

So I hope that was interesting and we've got a few more of these challenges coming up for you. 71

So keep your eyes peeled. 72

But in the next lesson, we're going to dive deeper into Javascript, and we're going to learn all about 73

control flow. 74

So you have all of that and more to look forward to. 75

So I'll see you on the next module.

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