Script simple math from weighted percentage sum from variable

Hello I'd like to show a percentage result from a weighted calculation of variables.
100% should be the total sum of variable 1, 2, 3 and 4 weighted by different % each.
Let's say:
variable 1 value is 10, weight is 20%
variable 2 value is 3, weight is 33%
variable 3 value is 50, weight is 10%
variable 4 value is 20, weight is 37%

How can I make these calcs on a script call?
tried: but doesn't work -->

$gameVariables.setValue(5, $gameVariables.value($gameVariables.value(1)/20*100+ $gameVariables.value(2)/33*100 + $gameVariables.value(3)/10*100 + $gameVariables.value(4)/20*100))
Hm, maybe I missed something, but if you want 20% shouldn't you be doing 20/100.

For example, you could write:
$gameVariables.value(4)/(20/100)
Or just do:
$gameVariables.value(4)/0.2
I think it's better to use only 1 variable, which will contain all values including their weights:
JavaScript:
$gameVariables.setValue(13, [
  { value: 10, weight: 0.2 },
  { value: 3, weight: 0.33 },
  { value: 50, weight: 0.1 },
  { value: 20, weight: 0.37 }
]);
This will store your data in variable #13.

From there, it's as simple as calculating value * weight / sum_of_all_weights. In this case, the sum_of_all_weights just happens to be 1 (100%), but let's use it anyway, so we can use arbitrary weights without having to calculate their percentage first.

JavaScript:
var data = $gameVariables.value(13);
var sumOfWeights = data.map(function(d) { return d.weight }).reduce(function(a, b) { return a + b; }, 0);

$gameVariables.setValue(14, data.map(function(d) { return d.value * d.weight / sumOfWeights; }));
This will store the weighted values from variable #13 into variable #14 (you could also store it back into variable #13 instead, if you want).

You can retrieve each data point like this:
JavaScript:
$gameVariables.setValue(15, $gameVariables.value(14)[0]);
This will store the first element from variable #14 into variable #15. In this case, the value is 2 (because 10 * 20% = 2).
JavaScript:
$gameVariables.setValue(16, $gameVariables.value(14)[1]);
This will store the second element from variable #14 into variable #16. In this case, the value is 0.99 (because 3 * 33% = 0.99).

And so on. Just remember that arrays (what we use to store multiple data into a single variable) are 0-based, so the first element has index 0, the second has index 1, etc. By contrast, RPG Maker variables and switches are 1-based, so the first variable/switch has index 1 instead.
Thank you for the very exstensive reply.
The thing with using only one variable is that I do not have absolute static values for those variables, but the are dynamic. The main goal here is to make a variable showing a % of game completion.
Hence, each of the 4 variables represent game achievements (let's say enemies, powerups, areas, levels and so on). I do not have a maximum number for them but I'll keep track of the values by increasing their relative nr. count variable. I'd like to get a final other variable value I can show anytime it's called, resulting in the % of completion. The average of the percentage is an additional plus, where unlock areas should be weighted more than enemies or powerups!

Original thread: https://forums.rpgmakerweb.com/threads/script-simple-math-from-weighted-percentage-sum-from-variable.141237/