Public
Edited
Oct 1, 2023
Insert cell
Insert cell
function onlyUnique(value, index, array) {
return array.findIndex(([v]) => v === value[0]) === index;
}
Insert cell
function calc(val, arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]) {
let options = [];
for (let i = 1; i < arr.length; ++i) {
for (let n = 0; n <= i; ++n) {
options.push([n / i, n, i]);
}
}
options = options.sort(([a], [b]) => a - b).filter(onlyUnique);
let prev = [0, 0, 0];
for (let o = 0; o < options.length; ++o) {
if (options[o][0] > val) {
if (options[o][0] - val > val - prev[0]) {
return [...prev, val - prev[0]];
} else {
return [...options[o], options[o][0] - val];
}
}
prev = options[o];
}
return prev;
}
Insert cell
calc(0.54)
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more