I have a problem. I need to find the element(s) in an array of ids causing a script to break. The script breaks without errors simply by freezing itself. I can unfreeze it by feeding it an array that doesn't contain the faulty element(s).
Here's my idea: I will keep splitting the array list in half over and over till I find each of the possible faulty elements. This approach should be faster than checking each one of them considering it takes 200ms to figure out if the script is frozen or not. Here is a simplified version of what I was considering doing. Is there an approach better than dividing lists by half and recursively running the same script on the smaller chunks?
const list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
const errors = [2, 7];
console.info("Faulty entries are", errors);
return findEl(list);
function findEl(array) {
console.info("Analyzing:", array);
const a = array.slice(0, Math.floor(array.length / 2));
const b = array.slice(Math.floor(array.length / 2));
for (const c of [a, b]) {
if (c.some(el => errors.indexOf(el) > -1)) {
if (c.length > 1) {
// Keep splitting chunks in half
findEl(c)
} else {
console.log("Found", c[0]);
}
}
}
}