title: Calculating the area of an irregular polygon using JavaScript
body: <p>I got this as an assignment, but I'm stuck on implementing the area portion.  The assignment is already turned in (finished about 80%).  I still want to know how to implement this, though. I am stuck on line 84-86.  Here is the prompt.</p>
  
  <p>Prompt: Calculate the area of irregularly shaped polygons using JS  </p>
  
  <p>INPUT: a nested array of 3-6 coordinates represented as [x,y]  (clockwise order)<br>
  OUTPUT: area calculated to 2 significant figures  </p>
  
  <p>Pseudocode:  </p>
  
  <ol>
  <li><p>loop the input and check for error cases:<br>
  a. array length &lt; 3 or array length > 6<br>
  b. array is not empty<br>
  c. numbers within -10 and 10 range  </p></li>
  <li><p>loop to each inner array, and multiply x and y in the formula below:  </p>
  
  <pre><code>sum_x_to_y = (X0 *Y1) + (X1* Y2)...X(n-1)* Yn
  sum_y_to_x = (Y0 * X1) + (Y1-X2)...Y(n-1)* Xn
  </code></pre>
  
  <p>ex:</p>
  
  <pre><code>(0, -10) (7,-10) (0,-8) (0,-10)
  | x  |  y |  
  | 0  |-10 |  
  | 7  |-10 |  
  | 0  |-8  |  
  | 0  |-10 |  
  
  sum_x_to_y =  0*-10 + 7*-8 + 0*-10 = -56  
  sum_y_to_x = -10*7 + -10*0 + -8*0 = -70  
  </code></pre></li>
  <li><p><code>area = (sum_y_to_x - sum_x_to_y) / (2.00)</code></p>
  
  <p>ex: <code>area = -70 -(-56) = 57/2 = 7</code></p></li>
  <li><p>return <code>area.toPrecision(2)</code> to have one sig fig</p>
  
  <pre><code>function PaddockArea(array_coords) {
      var sum_x_to_y = 0;
      var sum_y_to_x = 0;
      var arr_size = array_coords.length;
  
      if (arr_size === 0) {
          //check for empty array
          console.log("Invalid input.  Coordinates cannot be empty.");
      }
      if (arr_size &lt; 3 || arr_size &gt; 7) {
          //check input outside of 3-6 range
          console.log("Input out of range.");
      }
  
      for (var i = 0; i &lt; arr_size; i++) {
          for (var j = 0; j &lt; array_coords[i].length; j++) {
  
              //test for inner coordinates -10 to 10 range
              if (array_coords[i][j] &lt; -10 || array_coords[i][j] &gt; 10) {
                  console.log("Coordinates outside of -10 to 10 range.");
              }
  
              // I NEED TO IMPLEMENT TO calc for AREA here
              sum_x_to_y += array_coords[i][j] * array_coords[j][i];
              sum_y_to_x += array_coords[j][i] * array_coords[i][j];
              var area = (sum_y_to_x - sum_x_to_y) / 2;
  
              console.log(area.toPrecision(2) + "acres");
          }
      }
  }
  </code></pre></li>
  </ol>