Created
October 31, 2025 03:15
-
-
Save hasschi/615c73ce92c7a693c68a8659571d0d72 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function flex_calc(container, items = []){ | |
| var cs = (arr) => arr.reduce((p, c) => p + c, 0) - container; | |
| var t = items.slice(0); | |
| var s = cs(t.map(i => i[2])); | |
| if(s < 0){ | |
| var tg = t.reduce((p, c) => p + c[0], 0); | |
| if(tg == 0) return t.map(i => i[2]); | |
| return t.map(i => i[2] - s * i[0] / tg); | |
| } | |
| while(s > 0){ | |
| var tp = t.reduce((p, c) => p + c[1] * c[2], 0); | |
| t = t.map(i => [i[0], i[1], Math.max(0, (i[2] - s * i[1] * i[2] / tp).toFixed(2))]); | |
| s = cs(t.map(i => i[2])).toFixed(2); | |
| } | |
| return t.map(i => i[2]); | |
| } | |
| console.log(flex_calc(1000, [ | |
| [0, 1, 300], | |
| [0, 1, 400] | |
| ])); //[300, 400] | |
| console.log(flex_calc(1000, [ | |
| [1, 1, 300], | |
| [1, 1, 400] | |
| ])); //[450, 550] | |
| console.log(flex_calc(1000, [ | |
| [0, 1, 4000], | |
| [0, 1, 5000], | |
| [0, 1, 6000] | |
| ])); // [266.67, 333.33, 400] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment