Cuepoint selector

Select a cuepoint from an array based on a timestamp

by hyperthalamus

JavaScript

function getCuePointIndex(cuepoints, now) {
      var start = 0, end = cuepoints.length, index, ts;
      while (true) {
          index = ((start + end) >> 1);
          ts = cuepoints[index];
          if (index === end || index === start) { return index; }
          if (ts < now) { start = index; } // hasn't happened yet
          else if (ts > now) { end = index; } // has happened
          else { return index; } // is happening now
      }
  }
function getCuePoint(timestamp) {
    var myList = [1, 3, 6, 10, 14, 15, 18, 20, 25, 33, 36, 40, 43, 44, 48, 50];
    console.log("index of " + timestamp + " = " + getCuePointIndex(myList, timestamp));
}
console.log("------------------------");
getCuePoint(6);
getCuePoint(12);
getCuePoint(14.5);
getCuePoint(15);
getCuePoint(33);
getCuePoint(50);
getCuePoint(1);
getCuePoint(60);