Skip to content

Instantly share code, notes, and snippets.

@thedanielforum
Created May 31, 2019 03:47
Show Gist options
  • Save thedanielforum/f76db050c78104ef3fc32fa60c1cdd54 to your computer and use it in GitHub Desktop.
Save thedanielforum/f76db050c78104ef3fc32fa60c1cdd54 to your computer and use it in GitHub Desktop.
This code is a pice of 💩and should not be used unless you are planing to fix it up a bit.
// Input must be sorted in asc order
const input = [3, 8, 20, 25];
let output = [];
const minmax = [10, 30];
let distances = [];
for (let i = 0; i < input.length; i++) {
if ((i + 1) < input.length) {
if (input[i] > input[i + 1]) {
distances.push(input[i] - input[i + 1]);
} else {
distances.push(input[i + 1] - input[i]);
}
}
}
const stepSize = ((minmax[1] - minmax[0]) / distances.reduce((a, b) => a + b, 0));
for (let i = 0; i < input.length; i++) {
if (i === 0) {
output.push(minmax[0]);
} else {
let sumLowerSteps = 0;
for (let d = i; d > 0; d--) {
sumLowerSteps += distances[d - 1];
}
output.push((sumLowerSteps * stepSize) + minmax[0]);
}
}
console.log('input', input);
console.log('output', output);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment