Last active
June 28, 2022 19:04
-
-
Save rrfaria/ab418efb984c1c56e0711e8e908ad54e to your computer and use it in GitHub Desktop.
Carcular porcentagem de alocação da carteira de ações do primo rico
This file contains hidden or 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
| const itens = document.querySelectorAll('.tabela-carteira-kinvo tr') | |
| const acoes = [] | |
| itens.forEach(item => { | |
| const first = item.querySelectorAll('td:first-child') | |
| const last = item.querySelectorAll('td:last-child') | |
| var list = {} | |
| if (first.length){ | |
| list.asset = first[0].innerText | |
| const lastPrice = parseFloat(last[0].innerText.replace(/[R$.]/gm, '').replace(',', '.')) | |
| list.price = lastPrice | |
| acoes.push(list) | |
| } | |
| }); | |
| const total = parseFloat(acoes.reduce((a, b) => a + b.price, 0).toFixed(2)) | |
| acoes.map( item => { | |
| item.percent = parseFloat((item.price * 100 / total).toFixed(2)) | |
| }) | |
| itens.forEach((item, index)=> { | |
| if(index === 0) { | |
| item.appendChild(document.createElement('th')).textContent="Porcentagem" | |
| } | |
| if(index > 0 ) { | |
| item.appendChild(document.createElement('td')).textContent=acoes[index-1].percent + "%" | |
| } | |
| }) | |
Author
rrfaria
commented
Jun 28, 2022

Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment