given an array of numbers, what’s a good way to adjust one number and amortize the difference proportionally over certain of the other items in the array?
given:
var array = [10,20,30,40,50]
-> adjust the third item up by 30 and subtract proportionally across the first and last such that the sum of the items still totals 150 (second and fourth elements in the array are fixed or should remain the same).
= should come out to : [5,20,60,40,25]
(ideally javascript, but I could translate from C# / VB.net if that’s preferable)
Here’s a general purpose JS function to split the adjustment evenly across all the other items in the array:
If you really want a non-general function that just adjusts the ends and leaves the ones on either side of the middle untouched, then you would just do this:
I’m not sure I understand your proportional request, but here’s one that gives you the result you asked for:
You can see the last one work here: http://jsfiddle.net/jfriend00/QayQp/.