上面的图片是我正在研究的BMI计算器的屏幕截图。
下面的JavaScript代码应该将脚标签(ftheight)框中提供的值转换为英寸,然后将其添加到Inches标签中提供的值(inheight),然后将结果乘以2.54将其转换为厘米(厘米)。
给出的BMI结果是错误的,例如,体重是75kg,身高是5英尺9英寸,BMI结果显示0.31而不是24.42。但是当我将脚的值更改为0或空白时,将英寸值更改为69,它给出了24.42
的正确结果HTML
False
JAVASCRIPT
mask = (df1.notnull() & df2.notnull()).fillna(False)
print (mask)
A B C F
W False False False False
X False True False False
Y False True False False
Z False False False False
#if necessary change order of index values add reindex
df = df1.combine_first(df2).reindex(['X','Y','Z', 'W'])
print (df)
A B C F
X -1.0 1.0 2.0 NaN
Y 3.0 2.0 NaN -4.0
Z NaN NaN 4.0 NaN
W NaN -3.0 NaN -4.0
print (df.mask(mask))
A B C F
X -1.0 NaN 2.0 NaN
Y 3.0 NaN NaN -4.0
Z NaN NaN 4.0 NaN
W NaN -3.0 NaN -4.0
我想要的是首先将提供给脚盒的值转换为英寸,然后将其添加到英寸框中提供的值,并将结果乘以2.54,得到<form name="bmiForm">
Your Weight(kg): <br><br>
<input type="text" name="weight" size="10"><br />
Your Height: <br><br>
Feet: <input type="number" name="ftheight" size="10">
Inches: <input type="number" name="inheight" size="10"><br />
<input type="button" value="Calculate BMI" onClick="calculateBmi()"><br /><br />
Your BMI: <br><input class="bmiresult" name="bmi" size="10"><br />
This Means: <br><input class="bmimean" name="meaning" size="25"><br />
<input type="reset" value="Reset" /> </form>
的值
答案 0 :(得分:2)
我将权重输入更改为类型编号(以使其与其他输入保持一致),然后在输入值上调用parseFloat(因为输入值通常默认存储为文本,而不是数字)。 / p>
如果要将值限制为整数,则可以将此值更改为英制和英制值parseInt。
如果您发现输出是&#34;丑陋&#34;,您也可以使用toFixed约束您在结果中显示的小数位数。
function calculateBmi() {
var weight = parseFloat(document.bmiForm.weight.value);
var ftHeight = parseFloat(document.bmiForm.ftheight.value);
var inHeight = parseFloat(document.bmiForm.inheight.value);
var height = 2.54 * (ftHeight * 12 + inHeight);
if(weight > 0 && height > 0){
var finalBmi = weight / (height / 100 * height / 100)
document.bmiForm.bmi.value = finalBmi
if(finalBmi < 18.5){
document.bmiForm.meaning.value = "Hmm... you are too thin."
} else if(finalBmi > 18.5 && finalBmi < 25){
document.bmiForm.meaning.value = "Yah! you are healthy."
} else if(finalBmi > 25){
document.bmiForm.meaning.value = "Oops... you are overweight."
}
} else{
alert("Please Fill in everything correctly")
}
}
&#13;
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<form name="bmiForm">
Your Weight(kg): <br><br>
<input type="number" name="weight" size="10"><br />
Your Height: <br><br>
Feet: <input type="number" name="ftheight" size="10">
Inches: <input type="number" name="inheight" size="10"><br />
<input type="button" value="Calculate BMI" onClick="calculateBmi()"><br /><br />
Your BMI: <br><input class="bmiresult" name="bmi" size="10"><br />
This Means: <br><input class="bmimean" name="meaning" size="25"><br />
<input type="reset" value="Reset" />
</form>
</body>
</html>
&#13;
答案 1 :(得分:1)
我不确定你做错了什么,但这对我有用https://jsfiddle.net/vxshwk2w/
尝试使用id
代替name
。