Skip to content

Instantly share code, notes, and snippets.

@rrfaria
Last active June 28, 2022 19:04
Show Gist options
  • Select an option

  • Save rrfaria/ab418efb984c1c56e0711e8e908ad54e to your computer and use it in GitHub Desktop.

Select an option

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
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 + "%"
}
})
@rrfaria
Copy link
Author

rrfaria commented Jun 28, 2022

image

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