Last active
October 2, 2021 05:49
-
-
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.
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