<!DOCTYPE html>
<html>
<head>
<meta charset = "utf-8">
<title>BMI Calculator</title>
</head>
<body>
<h1>BMI Calculator</h1>
<form>
<label for = "height">Height (in meters):</label>
<input type = "text" id = "height"><br><br>
<label for = "weight">Weight (in kilograms):</label>
<input type = "text" id = "weight"><br><br>
<input type = "button" value = "Calculate BMI" onclick = "calculateBMI()">
</form>
<div id = "result"></div>
<script>
function calculateBMI() {
var height = parseFloat(document.getElementById("height").value);
var weight = parseFloat(document.getElementById("weight").value);
var bmi = weight / (height * height);
if (isNaN(bmi)) {
document.getElementById("result").innerHTML = "Please enter valid values for height and weight.";
return;
}
document.getElementById("result").innerHTML = "Your BMI is: " + bmi.toFixed(2);
if (bmi < 18.5) {
document.getElementById("result").innerHTML += "<br>You're underweight. Eat more!";
} else if (bmi >= 18.5 && bmi <= 24.9) {
document.getElementById("result").innerHTML += "<br>Congratulations! You have a healthy weight.";
} else if (bmi >= 25 && bmi <= 29.9) {
document.getElementById("result").innerHTML += "<br>You're overweight. Time to hit the gym!";
} else {
document.getElementById("result").innerHTML += "<br>You're obese. Consult a doctor.";
}
}
</script>
</body>
</html>
ジョーク:ダイエットする前に機械と相談すると、機械が「私たちは一緒に頑張りましょう!」と言ってくれる。