0

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?

6
  • You could set up all the potential values and their corresponding actions in an array of objects and then loop over the array, checking each object for a match and, when the match is found, perform its action. Commented Feb 12, 2019 at 22:09
  • @ScottMarcus could you provide an example? Commented Feb 12, 2019 at 22:10
  • First, can you explain how the statusOfBattle object could be equal to two properties of itself? Commented Feb 12, 2019 at 22:14
  • case is short for an === comparison. So you're testing statusOfBattle === (statusOfBattle.enemyDodged && statusOfBattle.enemyBlocked). That's clearly wrong. Commented Feb 12, 2019 at 22:21
  • @ScottMarcus if it was a recursive structure 😏 and javascript supported operator overriding. Clearly something is wrong though. Commented Feb 12, 2019 at 22:25

2 Answers 2

1

That's not how switch works - it evaluates the thing between parentheses, in your case statusOfBattle, and executes the case for the value equal to the thing. Your statement wouldn't match any case because a property of statusOfBattle won't be equal to the statusOfBattle object itself.

I don't completely understand how to determine who attacks, but regardless you could try using a simple if/else statement with some logical operators:

let doAttack = (firstAttacker, secondAttacker, statusOfBattle) => {
  statusOfBattle = createNonHitMessages(secondAttacker, firstAttacker, statusOfBattle);

  if (statusOfBattle.attackerDodged && !statusOfBattle.enemyBlocked) {
    return 'attacker attack';
  } else if (statusOfBattle.attackerBlocked) {
    return 'attacker attack';
  } else {
    return 'enemy attack';
  }
}

Make you can even make a shorter, better-looking version with an ? expression:

let doAttack = (firstAttacker, secondAttacker, statusOfBattle) => {
  statusOfBattle = createNonHitMessages(secondAttacker, firstAttacker, statusOfBattle);

  return statusOfBattle.attackerDodged ? 'attacker attack' : 'enemy attack';
}
Sign up to request clarification or add additional context in comments.

1 Comment

I was really hoping to avoid if statement but it seems there’s no other way. Hahahah.
0
switch (a) {
    case x:
        // do something
        break;
    case y:
        // do something else
        break;
    case z:
        // do third thing
        break;
}

is roughly equivalent to:

if (a === x) {
    // do something
} else if (a === y) {
    // do something else
} else if (a === z) {
    // do third thing
}

So your code won't work, because it's testing things like:

if (statusOfBattle === (statusOfBattle.enemyDodged && statusOfBattle.enemyBlocked))

Since statusOfBattle is an object and statusOfBattle.enemyDodged && statusOfBattle.enemyBlocked is either true or false, they will never be equal.

I'm not a big fan of this style, but you can do what you want by starting with:

switch (true) {
    ...
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.