Skip to content

Instantly share code, notes, and snippets.

@MathRivest
Last active June 15, 2017 17:42
Show Gist options
  • Save MathRivest/085b82cbd327e91dd4d51b3792b2ed91 to your computer and use it in GitHub Desktop.
Save MathRivest/085b82cbd327e91dd4d51b3792b2ed91 to your computer and use it in GitHub Desktop.
Rebalance script
let items = [
{
id: 1,
symbol: 'a',
cost: 25,
target: 50,
value: 500,
},
{
id: 1,
symbol: 'b',
cost: 15,
target: 25,
value: 200,
},
{
id: 1,
symbol: 'c',
cost: 15,
target: 25,
value: 200,
}
];
let cash = {
value: 300
}
getTotal = () => {
let total = cash.value;
items.forEach((item, index) => {
total = total + item.value;
});
return total;
}
balance = () => {
let newTotal = getTotal();
items.forEach((item, index) => {
let amountInCashNeeded = ((newTotal * item.target) / 100) - item.value,
amountInUnitNeeded = Math.floor(amountInCashNeeded / item.cost);
console.log(amountInCashNeeded, amountInUnitNeeded);
});
}
console.log(balance());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment