JSFiddle - React, Tailwind, and code Playground
by mschock
HTML
<div id="output"></div>
JavaScript
var input = [[0, 1], [3, 5], [4, 8], [10, 12], [9, 10]];
console.log('input:', input);
// assume ordering within pairs
function placePair(output, pair, lo, mid, hi) {
var currentPair = output[mid],
currentX = currentPair[0],
currentY = currentPair[1],
newX = pair[0],
newY = pair[1],
newMid;
if (newX >= currentX && newY <= currentY) {
// do nothing
} else if (newX >= currentX && newX <= currentY) {
// search right to place newY
} else if (newX > currentY) {
// search right to place pair
newMid = Math.floor((hi - mid) / 2);
placePair(output, pair, mid, newMid, hi);
} else if (newY <= currentY && newY >= currentX) {
// search left to place newX
} else if (newY < currentX) {
// search left to place pair
newMid = Math.floor((mid - lo) / 2);
placePair(output, pair, lo, newMid, mid);
} else { // newX < currentX && newY > currentY
// replace currentPair with pair
// search left to place newX
// search right to place newY
}
}
function placePoint(midpoint, point) {
}
function condense_meeting_times(input) {
var output = [];
for (var i = 0; i < input.length; i++) {
var pair = input[i],
lo = 0,
mid = 0,
hi = 0;
placePair(output, pair, lo, mid, hi);
}
return output;
}
var output = condense_meeting_times(input);
console.log('output: ', output);
var formattedOutput = (function(output) {
var formattedOutput = '[';
for (var i = 0; i < output.length; i++) {
formattedOutput += '(' + output[i].join(', ') + ')';
if (i < output.length - 1) {
formattedOutput += ", ";
}
}
return formattedOutput + ']';
})(output);
$('#output').html(formattedOutput);