Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Takes an array of numbers and returns an array of numbers in the original order which represent the percentage each number is of the sum of all of the numbers.
function getNumbersPercentageOfSum(numbers, decimalPlaces = 2) {
let total = 0;
for (const number of numbers) {
total += number;
}
return numbers.map(number => ((number / total) * 100).toFixed(decimalPlaces));
}
function getNumbersPercentageOfSum(
numbers: number[],
decimalPlaces: number = 2
): string[] {
let total = 0;
for (const number of numbers) {
total += number;
}
return numbers.map((number) =>
((number / total) * 100).toFixed(decimalPlaces)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment