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

267
Visualizações
How to refactor multiple variables and multiple if conditions

I am trying to build a graph using elk.js, and to be able to do that I need to have x value for each node. Nodes on the left should have x value smaller than those on the right.

  1. The graph contains nodes that are divided into rows.
  2. Nodes are created 1 by 1 so a check is done every time a new node is created
  3. Newly created nodes are added always on the right
  4. Numbers inside nodes are x values.
  5. Steps 1, 2, 3, etc. indicates the order in which we added nodes.
  6. I also listed a number of different conditions I need to check (this is something which I need help with)

enter image description here

What I currently have:

  1. I know x values on the right and on the left
  2. I know x values of children in the same parent node
  3. I know x values in each row

I have everything to make it work and in fact, it does work perfectly fine but my main issue is this code below. I am looking for a way to simplify this somehow and I am struggling a lot so any help would be appreciated.

If you have totally different approach than this one I will gladly know those.

if (allNodesFromCurrentRow.length === 0) {
  newNode.nodeLayout.x = parentX + 10;
} else {
  if (currentValue > 0 && rightValue === 0 && leftValue === 0) {
    newNode.nodeLayout.x = currentValue + 1;
  } else if (
    currentValue === 0 &&
    leftValue === 0 &&
    rightValue > 0
  ) {
    newNode.nodeLayout.x = rightValue - 1;
  } else if (currentValue === 0 && leftValue > 0 && rightValue > 0) {
    newNode.nodeLayout.x = (leftValue + rightValue) / 2;
  } else if (currentValue > 0 && leftValue === 0 && rightValue > 0) {
    newNode.nodeLayout.x = (rightValue + currentValue) / 2;
  } else if (
    leftValue > 0 &&
    currentValue === 0 &&
    rightValue === 0
  ) {
    newNode.nodeLayout.x = leftValue + 1;
  } else if (leftValue > 0 && currentValue > 0 && rightValue === 0) {
    newNode.nodeLayout.x = currentValue + 1;
  } else if (leftValue > 0 && currentValue > 0 && rightValue > 0) {
    newNode.nodeLayout.x = (rightValue + currentValue) / 2;
  }
}
about 4 years ago · Juan Pablo Isaza
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