Created
August 1, 2017 17:50
-
-
Save bmorelli25/2393b1c1a77fcb20b6f439e2071daab0 to your computer and use it in GitHub Desktop.
Bitcoin Chart Binary Search
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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