2012-09-19 28 views
1

我正在寫一個基本的程序在JavaScript中,它決定了如果有人將某個變量輸入到3個輸入中,會產生什麼樣的三角形。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 + inp2); 
sideB = (inp1 + inp3); 
sideC = (inp2 + 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> 

但是我認爲我錯過了一些東西,我似乎無法得到警報來告訴用戶任何東西。

謝謝大家,對不起張貼這個問題,它真的很困惑我

+1

您不能像這樣鏈接邏輯比較 - 像'(sideA == sideB)== sideC'那樣進行解析,它將真/假與第三方進行比較,並且不太可能是真實的。嘗試類似'sideA == sideB && sideB == sideC'。 – DCoder

+0

你好,感謝您的快速回復。我現在只是在嘗試。 這是否意味着對於等腰將是 if(sideA == sideB &&!= sideC) –

回答

0
  1. 你永遠調用checkinputs功能,所以形式永遠不會轉化成SIDEA,B,C。

  2. 您在equation函數中缺少a}。請記住正確縮進代碼以發現這些錯誤。

+0

非常感謝您的回覆,我將如何避免使用checkinput?這是在功能本身還是將是一個單獨的功能。 此外,我剛剛編輯公式,感謝您發現失蹤} –

+0

很多方式來處理。你可以從方程式()中調用它,或者你可以創建一個調用它們的新函數。 – sphair

+0

我剛剛編輯了上面的代碼。 所以內函數方程式(){在這裏調用它} –

0

你需要產業鏈的比較與邏輯運算符在一起,例如:

sideA == sideB && sideB == sideC 

,而不是

sideA == sideB == sideC 
2

嘗試使用此功能:

function getTriangleType(a,b,c) { 
    return (a === b && b === c) && 'equilateral' || 
    (a === b || a === c || b === c) && 'isosceles' || 
    'scalene'; 
} 

演示:http://jsbin.com/eyoxor/3/edit

+0

感謝您的迴應,它揭示了我將如何去做我的程序。然而,我現在正在使用其他方法:) 非常感謝你 –

+0

我的觀點是你不需要檢查'scalene',如果其他類型按照這個順序進行評估... – elclanrs

+0

謝謝你的回覆elclanrs :)一旦我真的得到它的工作,我會試驗你的方法 –