Skip to content

Instantly share code, notes, and snippets.

@dark4eg
Last active November 5, 2019 16:59
Show Gist options
  • Save dark4eg/3242f08ce0677f7c956ff9253bd3223f to your computer and use it in GitHub Desktop.
Save dark4eg/3242f08ce0677f7c956ff9253bd3223f to your computer and use it in GitHub Desktop.
const ITEMS_MAX = 3000000;
const calcPercent = (items) => {
if (items.length > 3000000) {
return null;
}
const digits = items.map(i => Number(i));
const sum = digits.reduce((a, item) => a + item, 0);
return digits.map(i => (100/(sum/i)).toFixed(3));
};
console.log('calcPercent params:', ['1.5', '3', '6', '1.5']);
console.log('calcPercent result:', calcPercent(['1.5', '3', '6', '1.5']));
console.log('calcPercent params:', ['23.56', '63.47', '96.12', '0.15', '40.22', '80.44']);
console.log('calcPercent result:', calcPercent(['23.56', '63.47', '96.12', '0.15', '40.22', '80.44']));
const simple = [...Array(3000000).keys()];
console.log('calcPercent result:', calcPercent(simple));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment