Skip to content

Instantly share code, notes, and snippets.

@bmorelli25
Created August 1, 2017 17:50
Show Gist options
  • Save bmorelli25/2393b1c1a77fcb20b6f439e2071daab0 to your computer and use it in GitHub Desktop.
Save bmorelli25/2393b1c1a77fcb20b6f439e2071daab0 to your computer and use it in GitHub Desktop.
Bitcoin Chart Binary Search
// d=data, t=target, s=start, e=end, m=middle
const binarySearch = (d, t, s, e) => {
const m = Math.floor((s + e)/2);
if (t == d[m].svgX) return d[m];
if (e 1 === s) return Math.abs(d[s].svgX t) > Math.abs(d[e].svgX t) ? d[e] : d[s];
if (t > d[m].svgX) return binarySearch(d,t,m,e);
if (t < d[m].svgX) return binarySearch(d,t,s,m);
}
let closestPoint = binarySearch(data,target, 0, data.length-1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment