Skip to content

Commit 4caa849

Browse files
Merge pull request #772 from IniyalPalanisamy/patch-6
Update script.js
2 parents 780680c + e7dad9c commit 4caa849

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

BMI Calculator (JS)/script.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
document.getElementById("btn").addEventListener("click", function() {
2-
var height_val = document.getElementById('height').value;
3-
var weight_val = document.getElementById('weight').value;
4-
var bmi = weight_val / (height_val / 100 * height_val / 100);
5-
var bmio = (bmi.toFixed(2));
2+
var height_val = document.getElementById('height').value; // in cm
3+
var weight_val = document.getElementById('weight').value; // in kg
64

7-
document.getElementById("result").innerHTML = "Your BMI is " + bmio;
8-
});
5+
// Convert height from cm to m
6+
var height_in_m = height_val / 100;
7+
8+
// Calculate BMI
9+
var bmi = weight_val / (height_in_m * height_in_m);
10+
11+
// Round BMI to 2 decimal places
12+
var bmio = bmi.toFixed(2);
13+
14+
document.getElementById("result").innerHTML = "Your BMI is " + bmio;
15+
});

0 commit comments

Comments
 (0)