title: Jarvis March implemention going into an infinite loop
body: <p>I implemented Jarvis march in javascript and then submitted it, turns out in some cases it goes into an infinite loop. I tried changing almost everything and it seems like the code keeps on going into an infinite loop just for a few test cases.</p>
  <p>This is my current code, the last for loop is to get all the points lying on the lines made by the convex hull points.</p>
  <pre class="lang-js prettyprint-override"><code>function convexHull(points, n)
          {
              // There must be at least 3 points
                  if (n &lt; 3) return;
  
                  // Find the leftmost point
                  let l = 0;
                  let hull = [];
                  for (let i = 1; i &lt; n; i++)
                      {
                          if (points[i].x &lt; points[l].x)
                              l = i;
                          else if (points[i].x == points[l].x &amp;&amp; points[i].y &lt; points[l].y)
                              l = i;
                      }
  
                  // Start from leftmost point, keep moving
                  // counterclockwise until reach the start point
                  // again. This loop runs O(h) times where h is
                  // number of points in result or output.
                  let p = l, q;
  
                  
                  do
                  {
  
                      // Add current point to result
                      hull.push(points[p]);
                      newhull.push(points[p]);
                      // Search for a point 'q' such that
                      // orientation(p, q, x) is counterclockwise
                      // for all points 'x'. The idea is to keep
                      // track of last visited most counterclock-
                      // wise point in q. If any point 'i' is more
                      // counterclock-wise than q, then update q.
                      q = (p + 1) % n;
  
                      for (let i = 0; i &lt; n; i++)
                      {
                         // If i is more counterclockwise than
                         // current q, then update q
                         if ((orientation(points[p], points[i], points[q])
                                                             == 2))
                             q = i;
                          if(p != i &amp;&amp; orientation(points[p], points[i], points[q]) == 0 
                             &amp;&amp; onSegment(points[p], points[q], points[i])) {
                             q = i;
                             }
                      }
                      
                      p = q;
  
                    }while (p != l);  // While we don't come to first point
                  
                    
  
                  for(let i = 0; i &lt; hull.length; i++) {
                    if(i + 1 &lt; hull.length) {
                    for (let j = 0; j &lt; points.length; j++) {
                      if(orientation(hull[i], points[j], hull[i+1]) == 0) {
                        newhull.push(points[j]);
                          points.splice(j, 1);
                          j--; }
                      }  
                    }
                    else if(i + 1 == hull.length) {
                      for (let j = 0; j &lt; points.length; j++) {
                        if(orientation(hull[i], points[j], hull[i+1]) == 0) {
                          newhull.push(points[j]);
                          points.splice(j, 1);
                          j--;
                        }
                      }
                    }
                  }
              }
  </code></pre>
  <p>Any help pointing out how I could improve this or get out of an infinite loop would be much appreciated.</p>