最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript triangle determine - Stack Overflow

programmeradmin2浏览0评论

I'm writing a basic programme in javascript which determines what kind of Triangle is being made if someone inputs a certain variable into either 3 inputs.

<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 puting, a parser is one of the ponents in an interpreter or 
    piler 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}

    .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>

However I think I'm missing something, I can't seem to get the alert to tell the user anything.

Thanks everyone, sorry for posting this question it's just really confusing me

I'm writing a basic programme in javascript which determines what kind of Triangle is being made if someone inputs a certain variable into either 3 inputs.

<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 puting, a parser is one of the ponents in an interpreter or 
    piler 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./js/js_parisons.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>

However I think I'm missing something, I can't seem to get the alert to tell the user anything.

Thanks everyone, sorry for posting this question it's just really confusing me

Share Improve this question edited Sep 19, 2012 at 10:19 Bradly Spicer asked Sep 19, 2012 at 9:29 Bradly SpicerBradly Spicer 2,3185 gold badges24 silver badges34 bronze badges 2
  • 1 You can't chain logical parisons like that - that gets parsed like (sideA == sideB) == sideC , which pares a true/false with the third side and is very unlikely to be true. Try something like sideA == sideB && sideB == sideC. – user1233508 Commented Sep 19, 2012 at 9:32
  • Hello, Thanks for your speedy response. I'm just trying that now. Would that mean that for the Isosceles it would be if (sideA == sideB && != sideC) – Bradly Spicer Commented Sep 19, 2012 at 9:36
Add a ment  | 

3 Answers 3

Reset to default 3

Try with this function:

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

Demo: http://jsbin./eyoxor/3/edit

  1. You are never calling checkinputs function, so the form is never converted into sideA,B,C.

  2. You are missing a } in your equation function. Remember to properly indent your code to spot these errors.

You need to chain your parisons together with logical operators, for example:

sideA == sideB && sideB == sideC

instead of

sideA == sideB == sideC
发布评论

评论列表(0)

  1. 暂无评论