bmi
<!DOCTYPE html>
<html>
<head>
<title>Js問題 CalcBmi</title>
</head>
<body>
<input type="text" id="weight" placeholder="体重 (kg)" oninput="InputJuge(this)">
<input type="text" id="height" placeholder="身長 (cm)" oninput="InputJuge(this)">
<button onclick="calcBmi()">計算</button>
<br>
<input type="text" id="result" placeholder="BMI" readonly>
<script>
function InputJuge(elementId) {
if (!/^[0-9]+$/.test(elementId.value)) {
elementId.value = elementId.value.replace(/[^0-9.]/g, '');
alert("半角数字を入力してください.");
}
}
function calcBmi() {
const weightValue = document.getElementById("weight").value;
const heightValue = document.getElementById("height").value;
const weight = parseFloat(weightValue);
const height = parseFloat(heightValue) / 100;
const bmi = weight / (height * height);
document.getElementById("result").value = bmi.toFixed(2);
}
</script>
</body>
</html>