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