Let's say there is an array of x numbers, and the sum of them are 100.
Each number increases or decreases linearly with a step. Each time a number increases the rest have to decrease evenly so that the sum of the numbers doesn't exceed 100. Likewise each time a number decreases the rest have to increase evenly so that the sum of the numbers doesn't go below 100. If a value is causing the sum to exceed or go below 100 the action has to be disallowed with a message to the user.
Let's say that array A: [20,20,20,20,20]
A[2] += 4
//the array A has to become somehow automatically: [19,19,24,19,19]
There is 3 problems on this. Firstly if a number exceeds a value, that will make the rest go below 0, and I don't want that. Example:
A: [-5,100,-5,-5,-5]
And the other one is related with the step. I don't know how much it should increase or decrease (maybe based on the length of the array).
Right now I have step = 1 / A.length
So that if a number increases with step the rest of them
have to decrease with step / A.length - 1
(minus one cause I don't count the number the user changed)
And vice-versa
Basically I am trying to do a percentage increment or decrement based on user value (down or up). Can you propose me the logic I have to follow, or some JavaScript code?
EDIT:
I have already implemented something in angular.
At first the values are all equal and sum is 100:
If i increase the first number with the button ( > ), the rest will decrease, and so on..
I am not posting code because its buggy and very meshed up, I just want the logic or a paradigm in code so I can implement it in my app. The check-boxes in the numbers are meant to lock that value so it doesn't increment or decrement.