Say I have user 1, user 2, and user 3. user 1 has 10x user 2 has 50x and user 3 has 40x. There is a reward and the reward is 1y. How is it possible to split that reward accordingly to the variables tied to each user? The user data is stored in a sqlite db.
Asked
Active
Viewed 50 times
-1
-
You mean dividing y to 100x and give them each 10%, 50% and 40%? – Apollo24 May 16 '20 at 09:23
-
yes, that is what I want but I don't know how to do that – 1AndOnlyPika May 16 '20 at 15:42
2 Answers
0
Try this:
const users = [
{name: 'user 1', weight: 10},
{name: 'user 2', weight: 50},
{name: 'user 3', weight: 40}
]
const reward = 200
const totalWeight = users.reduce((total, {weight}) => total += weight, 0)
const splitReward = users.reduce((obj, {name, weight}) => {
obj[name] = reward * weight / totalWeight
return obj
}, {})
console.log(splitReward)

Lauren Yim
- 12,700
- 2
- 32
- 59
0
I can't comment because I don't have enough reputation so I am posting it as answer
As per your statement,
100x of reward = 1y;
So the reward for a user with 1x is,
1x of reward = 1y / 100;
For example, if a user gets 40x reward, then
40x reward = 1y / 100 * 40
You can calculate the reward for any multiplier of x like this
(reward_multiplier)x reward = 1y / 100 * (reward_multiplier)

Anand Raj
- 435
- 2
- 8