简体   繁体   English

javascript和html逻辑语句

[英]javascript and html logic statements

So for a project I was assigned to make a simple html page that links to a javascript file. 因此,对于一个项目,我被分配做一个链接到javascript文件的简单html页面。 On this page there are 3 integer inputs and 2 buttons. 在此页面上,有3个整数输入和2个按钮。 The buttons test whether the integers will make a triangle or not, and the second button will test whether the integers make a right triangle. 按钮测试整数是否会形成三角形,第二个按钮测试整数是否会形成直角三角形。

Right now I am still having trouble with the first button. 现在,我仍然无法使用第一个按钮。 I am using the algorithm: 我正在使用算法:
a+b>c 一个+ B> C
a+c>b 一个+ C> B
b+c>a B + C>一个

if all of these are true then the statement should return as "It forms a triangle" and the else should state that it would not return a triangle. 如果所有这些都成立,则该语句应返回“它形成一个三角形”,否则将声明它不会返回三角形。

I think I have the logic and the set-up right but when I go to test all conditions will return true. 我认为我拥有正确的逻辑和设置,但是当我进行测试时,所有条件都将返回true。
Any help would be much appreciated. 任何帮助将非常感激。 Sorry if my code is kind of sloppy or doesn't make sense I'm still learning. 抱歉,如果我的代码有点草率或没有意义,我仍在学习。

Thanks again. 再次感谢。

HTML File: HTML档案:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Triangle Tester</title>
<script src="project3.js" type="text/javascript"></script>
</head>

<body>

<div>
<h1>Triangle Tester</h1>
<p>Please input the length (integer) of the first side of the triangle: </p>
    <input id="t1" type="text" size="3" />

<p>Please input the length (integer) of the second side of the triangle: </p>
    <input id="t2" type="text" size="3" />

<p>Please input the length (integer) of the third side of the triangle: </p>
    <input id="t3" type="text" size="3" />

<br/>
<span id="answer"></span>
<br/>

<button onclick="compute1();">Can these three sizes form a triangle?</button>
<button onclick="compute2();">Can these three sizes form a right triangle?</button>    
</div>

</body>
</html>

JavaScript File: JavaScript文件:

// JavaScript Document
function compute1()
{
    var in1 = document.getElementById("t1"); //A
    var in2 = document.getElementById("t2"); //B
    var in3 = document.getElementById("t3"); //C
    var ans = document.getElementById("answer");

    if( (in1.value+in2.value>in3.value)&&
        (in1.value+in3.value>in2.value)&&
        (in2.value+in3.value>in1.value) )
    {
        var result = "These three sides can form a triangle!";
        ans.innerHTML = result;
    }

    else
    {
        var result2 = "These three sides cannot form a triangle!";
        ans.innerHTML = result2;    
    }
}

function compute2()
{
    return 0;   
}

So any help would really be appreciated. 因此,任何帮助将不胜感激。

you will need to parse the values into numbers - at the moment all inputs are text and cannot be used to calculate the result correctly. 您将需要将这些值解析为数字-目前所有输入均为文本,无法用于正确计算结果。 Assuming your algorithm is correct, Use: 假设算法正确,请使用:

var in1Val=parseInt(in1.value);
var in2Val=parseInt(in2.value);
var in3Val=parseInt(in3.value);

    if( (in1Val+in2.value>in3Val)&&
        (in1Val+in3Val>in2Val)&&
        (in2Val+in3Val>in1Val) )...

if there is a risk that the input values might not be numerical, you can use parseFloat and check for NaN being returned (if there is a non-integer based input). 如果存在输入值可能不是数字的风险,则可以使用parseFloat并检查是否返回NaN(如果存在基于非整数的输入)。 Hope this helps Gavrif 希望这对Gavrif有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM