-1

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.

Syntle
  • 5,168
  • 3
  • 13
  • 34
1AndOnlyPika
  • 43
  • 1
  • 7

2 Answers2

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