Consider the following switch statement:
let doAttack = (firstAttacker, secondAttacker, statusOfBattle) => {
statusOfBattle = createNonHitMessages(secondAttacker, firstAttacker, statusOfBattle);
switch(statusOfBattle) {
case statusOfBattle.enemyDodged && statusOfBattle.enemyBlocked:
console.log('enemy Attack');
break;
case statusOfBattle.attackerDodged && statusOfBattle.attackerBlocked:
console.log('attacker Attack');
break;
case statusOfBattle.enemyDodged:
console.log('enemy Attack');
break;
case statusOfBattle.enemyBlocked:
console.log('enemey Attack');
break;
case statusOfBattle.attackerBlocked:
console.log('attacker attack');
break;
case statusOfBattle.attackerDodged:
console.log('attacker attack');
break;
default:
break;
}
return statusOfBattle
}
enemeyDodged, enemeyBlocked, attackerBlocked and attackerDodged will all return true or false.
I really do not want to write a giant if statement, I have nothing against it, aside from it doesn't look very clean when I have this many conditions.
if an if statement is the only way to go great, but I was hoping that I could check boolean values in a switch statement like this so I could keep this clean and readable.
I clearly don't understand the limitations of switch statements.
Any ideas?
statusOfBattleobject could be equal to two properties of itself?caseis short for an===comparison. So you're testingstatusOfBattle === (statusOfBattle.enemyDodged && statusOfBattle.enemyBlocked). That's clearly wrong.