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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getNumbersPercentageOfSum(numbers, decimalPlaces = 2) { | |
let total = 0; | |
for (const number of numbers) { | |
total += number; | |
} | |
return numbers.map(number => ((number / total) * 100).toFixed(decimalPlaces)); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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