我偶然发现自己在这个位置,我相信有一个更好的方式来做到这一点比我目前。更好的方法来编码递归如果语句
在这个例子中,我试图对一组有冲突项目的时间进行排序。我需要知道什么时候是高优先级的,不能移动的,与低优先级的,可以移动的。 但我很确定这段代码效率很低。
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
}
}
}
不幸的是,我甚至不知道怎么称呼这种类型的问题,因此不知道要寻找什么,但我敢肯定的答案不是太复杂(我希望不是)。
你可以使代码更易于通过使用一些中间变量与准确的名称 – 2010-12-18 01:47:07
什么是'sortedTimes'结构阅读(很可能更有效)? – 2010-12-18 01:48:05
哇,这是一个难以维护的代码块...... 10/10想要改进它......你一定要听从米奇小麦的建议 - 一旦你有中间变量,编辑你的Q与更新码。 – Basic 2010-12-18 01:51:51