在战斗机的生命值为假后,我的while循环不应该停止运行吗?



我相信结果是准确的,但是当它是错误的时,循环会继续运行。我什至将战斗机的生命值重置为零,以强制循环关闭,但它继续运行,直到两者都为零。如果您有任何建议,我会自己回去修复代码,但我非常困惑。谢谢

function Fighter(name, health, damagePerAttack) {
this.name = name;
this.health = health;
this.damagePerAttack = damagePerAttack;
this.toString = function() { return this.name; }
}


function declareWinner(fighter1, fighter2, firstAttacker) {
var result,
winner;
if (fighter1.name == firstAttacker) {
firstAttacker = fighter1;
battlev1();
} else if (fighter2.name == firstAttacker) {
firstAttacker = fighter2;
battlev2();
} else {
console.log(`${firstAttacker} isn't fighting right now!`)
return
}
function resetHealth(p) {
p.health = 0;
}
function resultsv1() {
if (firstAttacker.health <= 0) {
result = `${fighter2.name} attacks ${firstAttacker.name}; ${firstAttacker.name} now has ${firstAttacker.health} health and is dead. ${fighter2.name} wins`;
winner = fighter2.name;
console.log(winner);
resetHealth(fighter2);
} else if (fighter2.name <= 0) {
result = `${firstAttacker.name} attacks ${fighter2.name}; ${fighter2.name} now has ${fighter2.health} health and is dead. ${firstAttacker.name} wins`;
winner = firstAttacker.name;
console.log(winner);
resetHealth(firstAttacker);
}
}
function resultsv2(){
if (firstAttacker.health <= 0) {
result = `${fighter1.name} attacks ${firstAttacker.name}; ${firstAttacker.name} now has ${firstAttacker.health} health and is dead. ${fighter1.name} wins`;
winner = fighter1.name;
console.log(winner);
resetHealth(fighter1);
} else if (fighter1.name <= 0) {
fighter1.health = 0;
result = `${firstAttacker.name} attacks ${fighter2.name}; ${fighter2.name} now has ${fighter2.health} health and is dead. ${firstAttacker.name} wins`;
winner = firstAttacker.name
console.log(winner);
resetHealth(firstAttacker);
}
}
function determineLifeStatusv1() {
resultsv1();
}
function determineLifeStatusv2() {
resultsv2();
}
function fightv1() {
determineLifeStatusv1();
fighter2.health -= firstAttacker.damagePerAttack;
result = `${firstAttacker.name} attacks ${fighter2.name}; ${fighter2.name} now has ${fighter2.health} health.`;
console.log(result);
determineLifeStatusv1();
firstAttacker.health -= fighter2.damagePerAttack;
result = `${fighter2.name} attacks ${firstAttacker.name}; ${firstAttacker.name} now has ${firstAttacker.health} health.`;
console.log(result);
determineLifeStatusv1();
}
function fightv2() {
determineLifeStatusv2()
fighter1.health -= firstAttacker.damagePerAttack;
result = `${firstAttacker.name} attacks ${fighter1.name}; ${fighter1.name} now has ${fighter1.health} health.`;
console.log(result);
determineLifeStatusv2()
firstAttacker.health -= fighter1.damagePerAttack;
result = `${fighter1.name} attacks ${firstAttacker.name}; ${firstAttacker.name} now has ${firstAttacker.health} health.`;
console.log(result);
determineLifeStatusv2()
}
function battlev1() {
while (firstAttacker.health > 0 && fighter2.health > 0) {
fightv1();
}
}
function battlev2() {
while (fighter1.health > 0 && firstAttacker.health > 0) {
firstAttacker.health = 0;
fightv2();
}
}
}
declareWinner(new Fighter("Lew", 10, 2), new Fighter("Harry", 5, 4), "Harry");

问题出在你的函数fightv2。如果第一个战斗机死了,你必须完成功能。

function fightv2() {
determineLifeStatusv2()
fighter1.health -= firstAttacker.damagePerAttack;
result = `${firstAttacker.name} attacks ${fighter1.name}; ${fighter1.name} now has ${fighter1.health} health.`;
console.log(result);
// you have to quit function if fighter1 is dead, like so
if(fighter1.health <= 0) return;
determineLifeStatusv2()
firstAttacker.health -= fighter1.damagePerAttack;
result = `${fighter1.name} attacks ${firstAttacker.name}; ${firstAttacker.name} now has ${firstAttacker.health} health.`;
console.log(result);
determineLifeStatusv2()
}

最新更新