Click here to Skip to main content
15,899,026 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My problem is if we do not enter coordinates properly it should dispay "improper coordinates entered"
HTML
<html>
<head>
  <title>CALUCLATING AREA AND PERIMETER</title>
<center><h1>CALUCLATOR FOR AREA & PERIMETER</h1></center>

  <style>
  
  </style>

  <script language="JavaScript">

    var x = new Array();
    var y = new Array();
    var vertices;
    var digits;
    var area;
    var perimeter;
    var r;
    var carea;
    var cperi;
    var m;
    
    function readUserData()
    {
        
        vertices = parseInt( document.getElementById("VERTICES").value );
        if( (vertices < 2) || (vertices > 10 ) ) {
            alert( "Vertices must be >= 2 and <= 10" );
            return;
        }

       

        
       
        for( k = 0; k < vertices; k++ ) {
           if((x[k]==="undefined")&&(y[k]==="undefined"))
           {
             m=k+1;
           alert("improper coordinates entered at vertex position: "+m);
            exit(1);
           }
            x[k] = parseInt( document.getElementById("X"+k).value );
            y[k] = parseInt( document.getElementById("Y"+k).value );
        }
        
        x[vertices] = x[0];
        y[vertices] = y[0];
    }

    function calculateArea()
    {
        
        area = 0.0;

        for( k = 0; k < vertices; k++ ) {
            xDiff = x[k+1] - x[k];
            yDiff = y[k+1] - y[k];
            area = area + x[k] * yDiff - y[k] * xDiff;
        }
        area = 0.5 * Math.abs(area);
    }

    function calculatePerimeter()
    {
        

        perimeter = 0.0 
        for( k = 0; k < vertices-1; k++ ) {
            xDiff = x[k+1] - x[k];
            yDiff = y[k+1] - y[k];
            perimeter = perimeter + 
                        Math.pow( xDiff*xDiff + 
                        yDiff*yDiff, 0.5 );
        }
    }
function cir()
 {
 r= parseInt(document.getElementById("Y"+10).value);
 carea=3.1429*(r*r);
 cperi=6.2858*r;
alert(" circle area is "+carea);
alert(" circle perimeter is"+cperi);
}

    

    function handleCalculate()
    {
        readUserData();
        calculateArea();
        calculatePerimeter(); 
    
        alert("area is  "+area);
        alert("PERIMETER IS  "+perimeter);

    }

   
  </script>
</head>

<body>
<div align="center">
<div class="content">
<form>
<table border="2" cellspacing="0">
         cellpadding="2" style="border-collapse: collapse">

<tr>
<td colspan="2">Number of Vertices 
<input type="text" id="VERTICES" 
          size="5" value="0"></td>

</tr>
<tr>
<td align="center">Vertex</td>
<td colspan="2" align="left">X Value</td>
<td align="left">Y Value</td>
</tr>
<tr>
<td align="center">1</td>
<td colspan="2"><input type="text" 
               id="X0" size="20"></td>
<td><input type="text" id="Y0" size="20"></td>
</tr>
<tr>
<td align="center">2</td>
<td colspan="2"><input type="text" 
             id="X1" size="20"></td>
<td><input type="text" id="Y1" size="20"></td>
</tr>
<tr>
<td align="center">3</td>
<td colspan="2"><input type="text" 
             id="X2" size="20"></td>
<td><input type="text" id="Y2" size="20"></td>
</tr>
<tr>
<td align="center">4</td>
<td colspan="2"><input type="text" 
            id="X3" size="20"></td>
<td><input type="text" id="Y3" size="20"></td>
</tr>
<tr>
<td align="center">5</td>
<td colspan="2"><input type="text" 
           id="X4" size="20"></td>
<td><input type="text" id="Y4" size="20"></td>
</tr>
<tr>
<td align="center">6</td>
<td colspan="2"><input type="text" 
          id="X5" size="20"></td>
<td><input type="text" id="Y5" size="20"></td>
</tr>
<tr>
<td align="center">7</td>
<td colspan="2"><input type="text" 
         id="X6" size="20"></td>
<td><input type="text" id="Y6" size="20"></td>
</tr>
<tr>
<td align="center">8</td>
<td colspan="2"><input type="text" 
         id="X7" size="20"></td>
<td><input type="text" id="Y7" size="20"></td>
</tr>
<tr>
<td align="center">9</td>
<td colspan="2"><input type="text" 
         id="X8" size="20"></td>
<td><input type="text" id="Y8" size="20"></td>
</tr>
<tr>
<td align="center">10</td>
<td colspan="2"><input type="text" 
         id="X9" size="20"></td>
<td><input type="text" id="Y9" size="20"></td>
</tr>

<tr>
<td align="center"><input type="button" 
    value="Calculate"  önClick="handleCalculate()"></td>

<td align="center">
<input type="reset" value="Reset"></td>
</tr>
<tr><td>for circle:</td></tr>
<tr><td>ENTER radius <input type="text" id="Y10" size="20"></td></tr>
<tr><td><input type="button" value="circleCaluclator"  önClick="cir()"></td></tr>

</form>
</table></div>
</div>
</body>
</html>
Posted
Updated 8-Sep-11 22:29pm
v2
Comments
Timberbird 9-Sep-11 5:43am    
Have you tried to alert x[k] and y[k]? If their values are "undefined", try changing condition to x[k] === undefined or typeof(x[k]) == "undefined"
kesavahariprasad 9-Sep-11 6:58am    
tried even not working.i also changed the x[k] to integer and tried even not working
Timberbird 9-Sep-11 7:54am    
What does alert say?

1 solution

You are testing the values of x[k] and y[k] before you have created them with the parseInt command. Also you will only show the alert if both values are undefined, you should use the || (logical OR) operator.

[edit]You could also try reading this[^] and this[^].[/edit]
 
Share this answer
 
v3
Comments
Timberbird 9-Sep-11 7:56am    
I thought that was the idea of the OP - check what would be contained in the array before its elements are initialized. Actually, I'm a bit interested too :)
Richard MacCutchan 9-Sep-11 8:32am    
Why would OP want to do that? He (or she) creates a new array and then tests the value of an element before setting that element to a value; a rather pointless exercise, IMHO.
Timberbird 9-Sep-11 9:05am    
That's why I thought OP has written the code and now wants to check if everythings works fine, so he intentionally moved === test before assignment. Don't know where this decision comes from - maybe I was sure (esp. from "every thing write correct" header) that assignment misplacement is too obvious not to be intentional :). And it may - let's wait for the OP to explain
Richard MacCutchan 9-Sep-11 12:19pm    
let's wait for the OP to explain
Something one should always do.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900