Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

266
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda