html - 尝试用 HTML 制作 BMI 计算器,但 Inperial 单位不起作用
问题描述
我试图改变我的测量结果,但它仍然给了我古怪的答案。想知道我的代码中是否有任何问题。我不知道为什么,我没有任何错误。它所做的只是说计算后的BMI真的很高。这也是我的第一个堆栈溢出帖子,所以如果我有什么问题,请告诉我。
<html>
<head>
<title>BMI Calculator</title>
<script type="text/javascript">
function computeBMI() {
var height = Number(document.getElementById("height").value);
var heightunits = document.getElementById("heightunits").value;
var weight = Number(document.getElementById("weight").value);
var weightunits = document.getElementById("weightunits").value;
//Convert all units to metric
if (heightunits == "inches") height /= 3.93700787;
if (weightunits == "lb") weight /= 2.20462;
//Perform calculation
// var BMI = weight /Math.pow(height, 2)*10000;
var BMI = Math.round(weight / Math.pow(height, 2) * 10000);
//Display result of calculation
document.getElementById("output").innerText = Math.round(BMI * 100) / 100;
var output = Math.round(BMI * 100) / 100
if (output < 18.5)
document.getElementById("comment").innerText = "Underweight";
else if (output >= 18.5 && output <= 25)
document.getElementById("comment").innerText = "Normal";
else if (output >= 25 && output <= 30)
document.getElementById("comment").innerText = "Obese";
else if (output > 30)
document.getElementById("comment").innerText = "Overweight";
// document.getElementById("answer").value = output;
}
</script>
</head>
<body>
<h1>Body Mass Index Calculator</h1>
<p>Enter your height: <input type="text" id="height"/>
<select type="multiple" id="heightunits">
<option value="metres" selected="selected">metres</option>
<option value="inches">inches</option>
</select>
</p>
<p>Enter your weight: <input type="text" id="weight"/>
<select type="multiple" id="weightunits">
<option value="kg" selected="selected">kilograms</option>
<option value="lb">pounds</option>
</select>
</p>
<input type="submit" value="computeBMI" onclick="computeBMI();">
<h1>Your BMI is: <span id="output">?</span></h1>
<h2>This means you are: <span id="comment"> ?</span> </h2>
</body>
</html>
解决方案
推荐阅读
- python - 如何将一年中的某一天设置为几年的刻度标签?
- javascript - Discord.js - 模块导出不再起作用
- python - 我有一堆 python 打印功能。我想将输出写入文件
- c# - GraphQL HotChocolate 中 Url/Uri 的语法是什么?
- c# - 我如何能够取消显示的异步 oledb 查询?
- javascript - 使用命令向成员添加角色
- .net-core - 如何在窗口中加载剑道网格并为参数值使用隐藏字段
- node.js - 有没有办法从公共链接访问相册?
- wordpress - woocommerce如何根据重量调整价格?
- javascript - 使用正则表达式过滤掉包含 Javascript 文本中关键字的子字符串