Best way to encode recursive if statements

I am sometimes in this position, and I am sure that there is a better way to do this than I am now.

In this example, I am trying to sort a group times when I have conflicting elements. I need to know which times have high priority, cannot be moved, and also with low priority, can be moved. But I'm sure this code is very inefficient.

var holdLowPriorities = [];

for (conflict = 0; conflict < sortedTimes.length - 1; conflict++) {
    var firstConflictTimeStart = sortedTimes[conflict][0];
    var firstConflictTimeEnd = sortedTimes[conflict][1];
    var secondConflictTimeStart = sortedTimes[conflict + 1][0];
    var secondConflictTimeEnd = sortedTimes[conflict + 1][1];

    if (firstConflictTimeStart < secondConflictTimeEnd && 
            firstConflictTimeEnd > secondConflictTimeStart) {
        // are either of the conflicts a high priority
        var firstContactPriority = sortedTimes[conflict][2];
        var secondContactPriority = ortedTimes[conflict + 1][2]

        if (firstConflictPriority == 2) {
            //this is high priority, can't move
        }
        if (secondConflictPriority == 2) {
            // this is also a priority, but has to move
        }
        // are either of the conflicts a low priority?
        if (firstConflictPriority == 0) {
            // this is a low priority so I can adjust the time
        } else if (secondConflictPriority == 0) {
            // this is a low priority so I can adjust the time
        }
    }
}

Unfortunately, I don’t even know what to call this type of problem, and therefore I don’t know what to look for, although I’m sure that the answer is not too complicated (I hope anyway).

+3
source share
3 answers

A switch expression can help you cut the code a bit.

+2

: , .

.

var holdLowPriorities = [];

for(conflict=0; conflict<sortedTimes.length-1; conflict++){
  var conflictTime = sortedTimes[conflict],
      nextConflictTime = sortedTimes[conflict + 1];
  if (conflictTime[0] >= nextConflictTime[1] || conflictTime[1] <= nextConflictTime[0]) {
    continue;
  }

  // are either of the conflicts a high priority 
  if (data[conflictTime[2]].stepsArr[conflictTime[3]].priority==2) {
    alert(data[conflictTime[2]].stepsArr[conflictTime[3]].
  }
  if (data[nextConflictTime[2]].stepsArr[nextConflictTime[3]].priority == 2) {
    alert(data[nextConflictTime[2]].stepsArr[nextConflictTime[3]].
  }  

  // are either of the conflicts a low priority?
  if (data[conflictTime[2]].stepsArr[conflictTime[3]].priority==0) {
    holdLowPriorities.push([conflictTime[2], conflictTime[3], conflict]);
  } else if (data[nextConflictTime[2]].stepsArr[nextConflictTime[3]].priority == 0) {
    holdLowPriorities.push([nextConflictTime[2], nextConflictTime[3], conflict+1])
  }

  //alert(data[nextConflictTime[2]].stepsArr[nextConflictTime[3]].prerequisite+' '+conflictTime[0]+' '+conflictTime[1]+' '+nextConflictTime[0]+' '+nextConflictTime[1]+' '+data[nextConflictTime[2]].stepsArr[nextConflictTime[3]].taskid+' / '+data[conflictTime[2]].stepsArr[conflictTime[3]].taskid);
}

, .

function conflictData(conflict) {
    return data[conflict[2]].stepsArr[conflict[3];
}

var holdLowPriorities = [];

for(conflict=0; conflict<sortedTimes.length-1; conflict++){
  var conflictTime = sortedTimes[conflict],
      nextConflictTime = sortedTimes[conflict + 1];
  if (conflictTime[0] >= nextConflictTime[1] || conflictTime[1] <= nextConflictTime[0]) {
    continue;
  }

  var thisConflictData = conflictData(conflictTime),
      nextConflictData = conflictData(nextConflictTime);

  // are either of the conflicts a high priority
  if (thisConflictData.priority == 2) {
    alert(thisConflictData);
  }
  if (nextConflictData.priority == 2) {
    alert(nextConflictData);
  }

  // are either of the conflicts a low priority?
  if (thisConflictData.priority == 0) {
    holdLowPriorities.push([conflictTime[2], conflictTime[3], conflict]);
  } else if (nextConflictData.priority == 0) {
    holdLowPriorities.push([nextConflictTime[2], nextConflictTime[3], conflict+1])
  }

  //alert(nextConflictData.prerequisite + ' ' + conflictTime[0] + ' ' + conflictTime[1] + ' ' + nextConflictTime[0] + ' ' + nextConflictTime[1] + ' ' + nextConflictData.taskid + ' / ' + thisConflictData.taskid);
}

, , ; conflictTime, nextConflictTime , , , , else if. , ? , / . , , .

+2

if..else-. if..else . . , , ... ( , , ). ​​

, Mitch , . ( javascript ). , .

-, new Array(): holdLowPriorities.push([...]). , , , .

Thirdly, there are several places where all you do is check the priority of something. Use either a helper function or a method to simplify the code here: checkProirity(sortedTimes,conflict,2)or sortedTimes.checkProirity(conflict,2). Again, calling a function / method is inherently less efficient, but the point is to improve code clarity for good readability.

+1
source

Source: https://habr.com/ru/post/1780974/


All Articles