Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Nov 2004
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    JavaScript help - My IF, Else Statements not working

    I have a simple class project i can't seem to get working. The project is as follows. "Determine if 3 numbers could equal 3 sides of a triangle". To do this i have the user enter three numbers "A, B, C". After that, I add all the combinations up and compare them to one of the sides. If all of combinations are larger than the 3rd side, it is a triangle. See my code below. Please help.

    P.S I am sure there are more effiecient ways to make this code work, but i need to do it this way for class. Thanks again!





    ////Code starts here
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>Prime Number</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script language="JavaScript" type="text/javascript">
    function checkForTriangle() {
    num1 = parseInt(document.triangleForm.number1.value);
    num2 = parseInt(document.triangleForm.number2.value);
    num3 = parseInt(document.triangleForm.number3.value);
    sideA = (num1 + num2);
    sideB = (num1 + num3);
    sideC = (num2 + num3);
    if (sideA > num3)
    if (sideB > num2)
    if (sideC > num1)
    alert("These 3 sides CAN be a triangle");
    else
    alert("These 3 sides CAN NOT be a triangle");
    }
    </script>


    </head>

    <body>

    <form name="triangleForm" action="">
    <p>Enter number 1:
    <input type="text" name="number1"><br>
    <p>Enter number 2:
    <input type="text" name="number2"><br>
    <P>Enter number 3:
    <input type="text" name="number3"><br>
    <input type="button" value="Triangle or Not" onClick="checkForTriangle();">
    </p>
    </form>

    </body>
    </html>

    ////Code Ends here

  • #2
    fci
    fci is offline
    Senior Coder
    Join Date
    Aug 2004
    Location
    Twin Cities
    Posts
    1,345
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    <script language="JavaScript" type="text/javascript">
    function checkForTriangle()
    {
        num1 = parseInt(document.triangleForm.number1.value);
        num2 = parseInt(document.triangleForm.number2.value);
        num3 = parseInt(document.triangleForm.number3.value);
        sideA = (num1 + num2);
        sideB = (num1 + num3);
        sideC = (num2 + num3);
        if (sideA > num3 || sideB > num2 || sideC > num1) {
            alert("These 3 sides CAN be a triangle");
        } else {
            alert("These 3 sides CAN NOT be a triangle");
        }
    }
    </script>
    ?

  • #3
    New to the CF scene
    Join Date
    Nov 2004
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Still no go. I tried it, and now every variable i enter into the text fields comes back as "These three sides can be a triangle". Here is the whole code. Let me know if anyone can figure this out. Thanks!!!

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    <title>Triangle or Not</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script language="JavaScript" type="text/javascript">
    function checkForTriangle()
    {
    num1 = parseInt(document.triangleForm.number1.value);
    num2 = parseInt(document.triangleForm.number2.value);
    num3 = parseInt(document.triangleForm.number3.value);
    sideA = (num1 + num2);
    sideB = (num1 + num3);
    sideC = (num2 + num3);
    if (sideA > num3 || sideB > num2 || sideC > num1) {
    alert("These 3 sides CAN be a triangle");
    } else {
    alert("These 3 sides CAN NOT be a triangle");
    }
    }
    </script>


    </head>

    <body>

    <form name="triangleForm" action="">
    <p>Enter number 1:
    <input type="text" name="number1"><br>
    <p>Enter number 2:
    <input type="text" name="number2"><br>
    <P>Enter number 3:
    <input type="text" name="number3"><br>
    <input type="button" value="Triangle or Not" onClick="checkForTriangle();">
    </p>
    </form>

    </body>
    </html>

  • #4
    Regular Coder
    Join Date
    Jul 2002
    Posts
    698
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think it should be && not ||

    so that each side is always less than the 2 other sides added. but then again this kind of math has never been me.

  • #5
    New to the CF scene
    Join Date
    Nov 2004
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Everything is working great now.. Thanks for the help!!!

    -Dan


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •