执行此 If 语句的更好方法?

Better way to do this If statement?

非常简单:我编写了以下 if/else 语句:

   if (cr1.ins <= cr2.ins) {
     console.log("[ROUND 1] Creature 1 (#" + cr1.num + ") is attacking first.");
    cr2.hlt = cr2.hlt - (cr1.atk + cr2.ins);
    console.log("[HIT] Creature #" + cr2.num + " health reduced to " + cr2.hlt);
    if (cr2.hlt <= 0) {
      console.log("[DEATH] Creature #" + cr2.num + " Eliminated");
      remove(cr2);
    } else {
      console.log("[ROUND 2] Creature 2 (#" + cr2.num + ") is attacking second.");
      cr1.hlt = cr1.hlt - (cr2.atk + cr1.ins);
      console.log("[HIT] Creature #" + cr1.num + " health reduced to " + cr1.hlt);
      if (cr1.hlt <= 0) {
      console.log("[DEATH] Creature #" + cr1.num + " Eliminated");
      remove(cr1);
    }
    }
   } else {
    cr1.hlt = cr1.hlt - (cr2.atk + cr1.ins);
    console.log("[ROUND 1] Creature 2 (#" + cr2.num + ") is going first.");
    console.log("[HIT] Creature #" + cr1.num + " health reduced to " + cr1.hlt);
    if (cr1.hlt <= 0) {
      console.log("[DEATH] Creature #" + cr1.num + " Eliminated");
      remove(cr1);
    } else {
      console.log("[ROUND 2] Creature 1 (#" + cr1.num + ") is going second.");
      cr2.hlt = cr2.hlt - (cr1.atk + cr2.ins);
      console.log("[HIT] Creature #" + cr2.num + " health reduced to " + cr2.hlt);
      if (cr2.hlt <= 0) {
      console.log("[DEATH] Creature #" + cr2.num + " Eliminated");
      remove(cr2);
    }
    }
   }

我知道可能有更好的方法来执行此操作,因为 else{ } 中的代码与 if{ } 中的代码基本相同,但有一些变量名称更改,因此,有任何更改或重构的建议吗?我想在完成当前执行的相同任务的同时提高可读性和速度。

其实你可以简化这个,一般的方法是

if (cr1.ins > cr2.ins) {
  [cr2, cr1] = [cr1, cr2]; // just swap them!
}
attack(cr1, cr2);
if (cr2.hlt > 0) {
  attack(cr2, cr1);
}

对于带有 Creature 1/2 的日志记录语句,您还需要传递该信息,因此它可能变成类似于

const a = {designator: "Creature 1", creature: cr1},
      b = {designator: "Creature 2", creature: cr2};
const [first, second] = cr1.ins <= cr2.ins ? [a, b] : [b, a];
attack({attacker: first, defender: second, round: 1});
if (second.creature.hlt > 0)
  attack({attacker: second, defender: first, round: 2});

当然,如果你重构它使用上面的attack函数,再次写出if/else可能会变得更短。