const massMark = 78;
const heightMark = 1.69;
const massJohn = 92;
const heightJohn = 1.95;
const BMIMark = (massMark / heightMark ** 2).toFixed([1]);
const BMIJohn = (massJohn / (heightJohn * heightJohn)).toFixed([1]);
const markHigherBMI = BMIMark > BMIJohn;
// BMIMark > BMIJohn
if (markHigherBMI) {
console.log(`Mark's BMI (${BMIMark}) is higher than John's (${BMIJohn})!`)
} else {
console.log(`John's BMI (${BMIJohn}) is higher than Mark's (${BMIMark})!`)
}
// prints Mark's BMI (27.3) is higher than John's (24.2)!
참고로 여기서 소숫점 자리수를 제한하기 위해 .toFixed([digits])를 사용했다.
매개변수 digits에는 소수점 뒤에 나타날 자릿수를 지정해줌
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed
Number.prototype.toFixed() - JavaScript | MDN
toFixed() 메서드는 숫자를 고정 소수점 표기법(fixed-point notation)으로 표시합니다.
developer.mozilla.org
'Programming > JavaScript' 카테고리의 다른 글
Truthy and Falsy Values (0) | 2022.08.21 |
---|---|
Type Conversion and Coercion (0) | 2022.08.21 |
Taking Decisions: if / else Statements (0) | 2022.08.16 |
Strings and Template Literals (0) | 2022.08.09 |
Coding Challenge #1 - BMI Comparison (0) | 2022.07.10 |