Last active
March 2, 2018 03:43
-
-
Save tghelere/d9478a8e288c49b25e0f12fea17761aa to your computer and use it in GitHub Desktop.
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
//Este é o array que tenho | |
const array1 = [ | |
{ | |
"name": "João", | |
"lastname": "Afonso", | |
"participation": 12 | |
}, | |
{ | |
"name": "Maria", | |
"lastname": "Fonseca", | |
"participation": 77 | |
}, | |
{ | |
"name": "Ana Paula", | |
"lastname": "Santos", | |
"participation": 35 | |
}, | |
{ | |
"name": "Pedro Paulo", | |
"lastname": "de Oliveira", | |
"participation": 100 | |
} | |
] | |
//Este é o resultado que espero obter | |
const array2 = [ | |
{ | |
"fullName": "João Afonso", | |
"participationPercentage": 5 | |
}, | |
{ | |
"fullName": "Maria Fonseca", | |
"participationPercentage": 34 | |
}, | |
{ | |
"fullName": "Ana Paula Santos", | |
"participationPercentage": 16 | |
}, | |
{ | |
"fullName": "Pedro Paulo de Oliveira", | |
"participationPercentage": 45 | |
} | |
] | |
/** | |
* em um novo array | |
* >> Juntar os nomes | |
* >> calcular o percentual | |
* >> arredondar o valor | |
* | |
* | |
* | |
* | |
* A conta | |
* | |
* | |
* 12 + 77 + 35 + 100 = 224 (soma das participações) | |
* | |
* (regra de 3) | |
* 12 224 | |
* X | |
* y 100 | |
* | |
* y = 12 * 100 / 224 | |
* | |
* y = 5.357142857142857 | |
* | |
* y = 5 % (arredondado para 5) | |
* | |
* | |
* 5 + 34 + 16 + 45 = 100 % (só para garantir, soma dos percentuais resultando em 100) | |
* | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment