Skip to content

Commit 6df687f

Browse files
committed
done bmi.js
1 parent 0f98c63 commit 6df687f

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

Sprint-2/implement/bmi.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,38 @@
1313
// Given someone's weight in kg and height in metres
1414
// Then when we call this function with the weight and height
1515
// It should return their Body Mass Index to 1 decimal place
16+
17+
// ============================= BMI TEST ===============================
18+
19+
function bodyMassIndex(weight, height) {
20+
const kilograms = Number(weight);
21+
const metres = Number(height);
22+
23+
if (kilograms > 0 && metres > 0) {
24+
const bmi = kilograms / metres ** 2;
25+
return Math.round(bmi * 10) / 10;
26+
}
27+
return "Invalid input. Please enter valid weight and height.";
28+
}
29+
30+
31+
//Avoid .toFixed() if we need a number (as .toFixed() returns a string).
32+
console.log(bodyMassIndex(70, 0));
33+
34+
35+
/*
36+
Dynamic Precision with Math.round()
37+
Combining Math.pow() with Math.round() offers a dynamic solution for those
38+
seeking a more flexible approach that can adapt to rounding to various decimal
39+
places.
40+
41+
function roundTo(num, precision) {
42+
const factor = Math.pow(10, precision)
43+
return Math.round(num * factor) / factor
44+
}
45+
console.log(roundTo(4.687, 0)); // Output: 5
46+
console.log(roundTo(4.687, 1)); //one decimal place
47+
console.log(roundTo(4.687, 2)); //two decimal places
48+
console.log(roundTo(4.687, 3)); // Output: 4.687
49+
50+
*/

0 commit comments

Comments
 (0)