All right. So we know that the formula is weight in kilograms divided by height squared, and we have to
make this line of code work.
So let's first try it out in the calculator.
Let's say that we first try to square our height which is 1.8 meters.
So on a calculator we can simply write 1.8 times 1.8.
So 1.8 squared is equal to 3.24.
So now we can divide 65 by 3.24. 65
divide by 3.24 and we get 20.0617 something something something.
All right. So now that we know that this is how we would calculate it on a calculator, then we can simply
copy all of this starting code, and open up our Chrome Developer Tools, and paste it into our Snippets
tool.
So now, we're going to create our function below this line.
And the first step to creating any sort of function is the function keyword,
right? Then we're going to give our function a name, which we said has to be the name bmiCalculator,
so we're going to use that name.
Next step is some parentheses, and whether we're going to add some inputs in here.
So in this case we're saying that put the input weight first and then put the input height.
So our inputs are going to be weight and height.
Now we get to open up our curly braces and specify everything that this function is going to do.
So this function has to calculate the value of bmi.
Remember that the formula is the weight divided by height squared.
So bmi is going to equal to the weight input divided by the height input multiplied by itself.
And remember, in the previous lesson we talked about Javascrpit numbers, that in order to specify the
order of your calculations then you can use the parentheses to set precedence.
So by putting those two parentheses there then we say that calculate height times height first and then
divide weight by that value.
So now in order to get value bmi out of this function, we have to use the return keyword, and we can say
return bmi.
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
I hit Check solution, then you can see that it will pass.
Now there's a number of different ways of writing this function.
Some of you might have wondered,
I don't really want to multiply the height by height in order to get it squared.
Why can’t I just raise height to the power of 2?
How would I do that in Javascript?
Well, as with any query regarding programming, the solution is only a quick google away.
So if we search for ‘raise a number to a power’ and we add the keyword Javascript, then you can see that the
MDN docs come up with the Math.pow function, and it shows us how to use it.
It says that we can simply say Math.
pow,
and the first number is the base and the second number is the exponent.
So this is your squared which will be 2 or cubed which will be 3.
And this is how you would use it.
So let's try it out
in our case. Instead of writing height times height,
what if we did Math.
pow, set of parentheses, and then we said height, 2, so height to the power of 2?
Now, if we check our solution, you can see that our function is still working exactly as we expect it
to,
but it's now perhaps a little bit more elegant.
Now, if you run this code as it is right now, for example we're calculating the BMI from 65 kilograms
and 1.8, and then we're logging that BMI into the console,
then you can see that we're getting a really long number,
20.0672839 etc..
Now usually with BMI we're working with whole numbers.
So how can we turn this long number with loads of decimal places to simply a whole number?
Well again Google is your friend.
And if you search for ‘Javascript round a number to the nearest whole number’ or something around there, then
the first result that you get is from W3 Schools, and it tells you about the round method, which you can
use by saying Math.round, and is similar to the floor method, which simply rounded down your decimal
place.
But Math.round is more mathematically correct.
So if it's 2.5 it will round it to 3, and if it's 2.4
it’ll round it to 2.
So let's try using this in our solution.
So if, instead of returning bmi, we returned Math.round, and inside the parentheses we enclosed our
bmi, as a number with decimal places.
Now if we run our code you can see that we only get 20 back which is exactly what we wanted.
And if you change our solution to this updated one inside our testing suite, then you can see that our solution
also passes.
So I hope that was interesting and we've got a few more of these challenges coming up for you.
So keep your eyes peeled.
But in the next lesson, we're going to dive deeper into Javascript, and we're going to learn all about
control flow.
So you have all of that and more to look forward to.
So I'll see you on the next module.