let's say I have an arbitrary number sequence
let sequence = [
0, 0, 0, 0, 0,
12, 64, 9, 6,
0, 0, 0,
25, 79, 57, 13, 39,
0, 0,
7, 7,
0, 0, 0, 0, 0,
49,
0
];
I need to replace all zeroes with interpolation given from non-zeroes neighbours, so the output would be
let output = [
12, 12, 12, 12, 12,
12, 64, 9, 6,
10.75, 15.5, 20.25,
25, 79, 57, 13, 39,
28.3333, 17.6666,
7, 7,
14, 21, 28, 35, 42,
49,
49
];
While firs zeroes [0, 4] doesn't have left neighbour all their values have to be 12, while last zero has only right resident 49, it would be just 49.
For me, it doesn't really a problem to fill parts where both left and right neighbours are presented, however I'm looking for an universal and elegant solution for this task.
const interpolateValues = (array, index0, index1, left, right) => {
let n = index1 - index0 + 1;
let step = (right - left) / (n + 1);
for(let i = 0; i < n; i++){
array[index0 + i] = left + step * (i + 1);
}
}
const findZerosSequences = (array) => {
var counter = 0;
var index = 0;
var result = [];
for (let i = 0; i < array.length; i++) {
if (array[i] === 0) {
index = i;
counter++;
} else {
if (counter !== 0) {
result.push([index - counter + 1, index]);
counter = 0;
}
}
}
if (counter !== 0) { result.push([index - counter + 1, index]); }
return result;
}
let sequence = [
0, 0, 0, 0, 0,
12, 64, 9, 6,
0, 0, 0,
25, 79, 57, 13, 39,
0, 0,
7, 7,
0, 0, 0, 0, 0,
49,
0
];
//[[0,4], [9, 11], [17, 18], [21, 25], [27, 27]]
let zeroes = findZerosSequences(sequence);
for(let i = 0; i < zeroes.length; i++){
let lf = sequence[zeroes[i][0] - 1];
let rf = sequence[zeroes[i][1] + 1];
if(lf !== undefined && rf !== undefined && lf > 0 && rf > 0){
interpolateValues(sequence, zeroes[i][0], zeroes[i][1], lf, rf);
}
}
console.log(sequence);
let output = [
12, 12, 12, 12, 12,
12, 64, 9, 6,
10.75, 15.5, 20.25,
25, 79, 57, 13, 39,
28.3333, 17.6666,
7, 7,
14, 21, 28, 35, 42,
49,
49
];