Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

263
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!