numbers within an array. I am mostly looking to implement something like this: https://softwareengineering.stackexchange.com/questions/261017/algorithm-for-a-ui-showing-x-percentage-sliders-whose-linked-values-always-total
Example:
max: 9,
3 values
I want to set the first item to 4, so the rest has to redistribute.
[3,3,3] -> [4,2,3]
or [8,0,2] (set second item to 2) -> [7, 2, 1] (take one from first and last item)
I don't even know how to start tackling this problem :D
This is how far I got:
const distribute = (arr: number[], max: number, index: number, newVal: number) => {
return arr.map((a, i, arr) => i == index ? newVal : a)
}
This
Обсуждают сегодня