// asAtDate must be a string of type yyyymmdd // function returns price, or -1 if out of range function getHistoricalPrice(asAtDate,asxCode) { var count = wlWDCDateArr.length; var date = parseInt(asAtDate); if (date > wlWDCDateArr[count-1] || date < wlWDCDateArr[0]) return -1; var lower = 0; var upper = count-1; var index = parseInt(upper/2); var curDate; // binary search for date while(upper - lower > 1) { curDate = wlWDCDateArr[index]; if (curDate == date) return wlWDCPriceArr[index]; if (curDate > date) upper = index; else lower = index; index = parseInt((upper+lower)/2); } // Make sure that what we've found is actually the right day. if (wlWDCDateArr[upper] == date) { return wlWDCPriceArr[upper]; } else { return -1; } if (wlWDCDateArr[lower] == date) { return wlWDCPriceArr[lower]; } else { return -1; } }