I have an array like this:
[0, 24.646213151927437, 27.737256235827665, 29.08326530612245]
I want to fill every space between the elements which are greater than 4 with random numbers. Like this:
var arr = [0, 24.646213151927437, 27.737256235827665, 29.08326530612245];
var length = arr.length;
for (var j = 0; j < length;) {
var spacebetween = arr[j + 1] - arr[j];
if (spacebetween > 4) {
// fill the space until the sum of numbers is greater than space between
var m = Math.random() * (3 - 1 + 1);
for (k = m; k < spacebetween;) {
arr.splice(j, 0, parseFloat(k));
m = Math.random() * (3 - 1 + 1);
k = k + m;
}
}
j++;
}
console.log(arr);
The problem is with splice(), the index is shifting and I have no idea how to solve this. The new numbers should stay in ascending order within the array.