-1

I was going to calculate BMI with javascript code in which I've created two variables named height and weight.

You may know the formula of getting the BMI is, BMI = Weight(kg) / (Height(m))^2;

but I got right answer only after writing Height*Height. height*height gives exact answer but height^2 gives answer with rounded-off value.

So I want to know why the operator or symbol "^" is rounding off the value that I want to square.

  • 2
    Not sure about java script. but `^` is hardly ever exponentiation... Check the operator specification in the language of your choice. Or instead of `(X^2) ` try `(X*X)`. – Yunnosch Jul 27 '22 at 06:23
  • Ya It's (x**2). Here ** is used as exponent operation. – Shubham Ingole Jul 27 '22 at 13:49

1 Answers1

2

You must to use Math.pow

"^" this is not squaring

Code for your example:

const weight = 100
const height = 300

const result = weight / Math.pow(height, 2)
// The same as going height^2, height to the power of 2
Nikita Aplin
  • 367
  • 2
  • 10
  • Yes your right but I want to know about exponent operator, but later on I just research and found that (**) is used as exponent operator. Thanks for your answer. – Shubham Ingole Jul 27 '22 at 13:51