Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

256
Visualizações
How to add the missing numbers with this code and only doing it when a variable is 0 in angular 7

What this functionality does is that for each priority it will rebalance the percentage (Participacion) in equal parts. The problem occurs in odd cases, for example when there are 3 priorities(Prioridad), you must divide 100 / 3, which gives a result of 33 (and 33 + 33 + 33 = 99 )

What should I add to my code, so that when the result of the division is not 100, the missing numbers are added to the last position, completing the 100.

Example problem 1 : Example gif : The idea is that the last 33 that you see in the gif is automatically transformed into a 34.Tthis is the code i have so far:

rebalance(){
    this.beneficiarios = this.beneficiarios.sort((a, b) =>Number(a.Prioridad) - Number(b.Prioridad)).map((val, i, beneficiarios) => {
      const priorityLenght = this.beneficiarios.filter((a) =>
      Number(a.Prioridad) == Number(val.Prioridad)).map((b) =>
      Number(b.Participacion)).length
        const arrObj = {
          ...val,
          Participacion : 100 / priorityLenght
        }
        return arrObj;
      })
  }

Problem 2: In the previous gif you can see that when you press the button, the rebalancing was done for all the priorities (Prioridad), I would like it to be only for those that contain a 0 in percentage (Participacion)

I'm pretty sure that both problems can be solved by adding a couple lines of code to the function I put above, but I lack experience to know what to do

Here a StackBlizz of the proyect: https://stackblitz.com/edit/create-a-basic-angular-component-v8k1hs?file=src/app/example/user.component.ts

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I fixed your algorithm. Really just needed a slightly different approach. I changed it to use Math.floor() for participation and then got the diff (100 % length) needed to make the value 100% and added that to the last value. Here is the updated stackblitz.

https://stackblitz.com/edit/create-a-basic-angular-component-7vj1rr?file=src/app/example/user.component.html

PS: never make method calls in the template due to the overhead the this add each time change detection runs; instead use pure pipes or calculate their value for presentation when the data changes. There was a decimal formatting method being called that I removed.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda