Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@edazpotato
Last active October 2, 2021 05:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edazpotato/12f1d2002455236a9021f4fd2d304727 to your computer and use it in GitHub Desktop.
Save edazpotato/12f1d2002455236a9021f4fd2d304727 to your computer and use it in GitHub Desktop.
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