【发布时间】:2016-10-28 20:38:00
【问题描述】:
<body>
<pre>
<form>
<strong> Height:</strong> <input id ="Height"><br/>
<strong> Base: </strong> <input id ="Base"><br/>
<strong> Hypotenus: </strong> <input id ="Hypotenus"><br/>
</form>
<button type ="Button" onclick="Evaluate()">Find Area</button>
<p id ="Answer"> Answer will appear here </p>
<script>
function Evaluate() {
var H = document.getElementById("Height").value;
var B = document.getElementById("Base").value;
var Hy = document.getElementById("Hypotenus").value;
if (B == NaN || null) {
var Area = Math.sqrt(Math.pow(Hy, 2) - Math.pow(H, 2));
}
document.getElementById("Answer").innerHTML = Area;
}
</script>
</body>
我是 JavaScript 新手,我一直在尝试编写一个找到三角形公式的代码。我的问题是在 if 语句之后我想更改“Area”的值,但每次运行代码时我都会收到undefined。如何在 if 语句中更改变量的值?
【问题讨论】:
-
首先,您首先使用适当的条件,如
if ( B == NaN || B == null ),然后您意识到元素值将永远为 NaN 或 null,因为它始终是一个字符串.
标签: javascript function variables if-statement scope