-1

最近我问了一个关于我用 JavaScript 编码的三角形计算的问题。基本上我去了 JSLint(一个 JavaScript 调试工具)来获取一些数据,以了解为什么函数和过程中没有任何东西在看起来不错的地方工作,并且我遵循了之前给出的建议,但我相信这是一个单独的问题。我已经发布了下面的代码:

    <html>
<head>
<link href="stylesheet/stylesheet.css" rel="stylesheet" type="text/css">

<script language="Javascript" type="text/javascript">

/*  Key/Legend
Var
    inp1 = input1
    inp2 = input2
    inp3 = input3
    Triangle_Inputs = Form Name

    */

/* Notes

    In computing, a parser is one of the components in an interpreter or 
    compiler that checks for correct syntax and builds a data structure 
    (often some kind of parse tree, abstract syntax tree or other hierarchical structure) 
    implicit in the input tokens.


    Technique
    if (side1 is equal to side2 AND side 2 is equal to side3) {equalitateral}

    if (side1 is equal to side2 AND side 2 doesn't equal to side3) {isosceles}

    if (side1 doesn't equal to side2 AND side2 doesn't equal to side 3 AND side 3 doesn't equal side 1) {scalene}

    http://www.w3schools.com/js/js_comparisons.asp

    */


function checkinputs(){
/* Var = parseInt(document.Name_Of_Element_Form.Field_Name(Input).value); */
/* Input Fields */
inp1 = parseInt(document.Triangle_Inputs.input1.value);
inp2 = parseInt(document.Triangle_Inputs.input2.value);
inp3 = parseInt(document.Triangle_Inputs.input3.value);
/* Side options */
sideA = (inp1);
sideB = (inp2);
sideC = (inp3);
    if (sideA == sideB && sideB == sideC) {
    alert("Equalateral");
    }
    if (sideA == sideB && != sideC) {
    alert("Isosceles");
    }
    if (sideA != sideB == sideC) {
    alert("Isosceles");
    }
    if (sideA != sideB != sideC != sideA) {
    alert("Scalene!");
    }
}
</script>


</head>
<body>
<div id="Container">

<div id="Header"><h1></h1></div>

        <div id="Content_1">
                <div id="Explanation">
                This calculator will determine what
                triangle you have made depending on
                the integer values in the input fields.

                </div>
                <div id="Form">
                    <FORM NAME="Triangle_Inputs" METHOD="GET">
                    Enter the triangle values below: <br>
                    <p>
                    <h4>Side 1: </h4><BR>
                    <INPUT TYPE="Integer" NAME="input1" VALUE=""><P>
                    <h4>Side 2: </h4><BR>
                    <INPUT TYPE="Integer" NAME="input2" VALUE=""><P>
                    <h4>Side 3:</h4> <BR>
                    <INPUT TYPE="Integer" NAME="input3"  VALUE=""><P>
                    <INPUT TYPE="button" NAME="Submit" Value="Submit" Class="Submit" onClick="checkinputs()">
                    </FORM>
                </div>
                <div id="Verbal_Output">
                    <h2>You made a:</h2>
                    <p>        
                    <h2>Triangle</h2>
                </div>

            </div>
            <p>
            <p>
        <div id="Content_2">

        <div id="Image_Output">asdad</div>
    </div>      
</div>



</body>
</html>

这是网站本身,忽略 div 标签。我想知道为什么我不能点击按钮以及更多 JSlint 说的重点:

inp1 = parseInt(document.Triangle_Inputs.input1.value);

第 39 行字符 1 'inp1' 在定义之前被使用。

对于 inp1、inp2 和 inp3。

编辑:

我对使用变量的理解是不正确的,我列出它们时没有使用“,”。相反,我调用了一个变量,其余的我没有。

4

1 回答 1

1

试试这个:

var checkinputs = function() {
/* Input Fields */
var sideA = parseInt(document.Triangle_Inputs.input1.value),
    sideB = parseInt(document.Triangle_Inputs.input2.value),
    sideC = parseInt(document.Triangle_Inputs.input3.value);

alert (sideA == sideB && sideB == sideC) ? 'Equilateral' :
      (sideA == sideB && sideA != sideC) ? 'Isosceles' :
      (sideA != sideB && sideA == sideC) ? 'Isosceles' :
      (sideA != sideB && sideA != sideC && sideB != sideC) ? 'Scalene' :
      'logic needs improvement';
}

我还注意到你的逻辑有点缺陷,所以我把它编辑成我认为正确的样子。

于 2012-09-19T10:58:08.280 回答