if {} else if {if {} else {}}的三元条件

时间:2019-04-11 07:55:15

标签: javascript conditional-statements ternary

我想将所有if else语句更改为三元运算符。此if else语句的三元运算符是什么?

  const compareHands = (playerChoice, computerChoice) => {
        // Update Text
        const winner = document.querySelector('.winner');
        const winnerIs = (who, isPlayerWin) => {

            winner.textContent = `${who} Wins!`;

            isPlayerWin ? pScore++ : cScore++;

            updateScore();
        };



        // Check for tie
        if (playerChoice === computerChoice) {
            winner.textContent = 'It Is A Tie!';
            // Check For Winner
        } else if (playerChoice === 'rock') {
            if (computerChoice === 'scissors') {
                winnerIs('Player', true);
            } else {
                winnerIs('Computer', false);
            }
        } else if (playerChoice === 'paper') {
            if (computerChoice === 'scissors') {
                winnerIs('Computer', false);
            } else {
                winnerIs('Player', true);
            }
        } else if (playerChoice === 'scissors') {
            if (computerChoice === 'rock') {
                winnerIs('Computer', false);
            } else {
                winnerIs('Player', true);
            }
        }
    }

5 个答案:

答案 0 :(得分:1)

正如Nina Scholz所说,我也不会使用。我知道这不能回答字面上的问题,但是如何解决呢?

const loser_to = {
  paper: 'rock',
  rock: 'scissors',
  scissors: 'paper'
};

if (loser_to[playerChoice] === computerChoice) {
  // player wins
} else if (loser_to[computerChoice] === playerChoice) {
  // computer wins
} else {
  // noone wins
}

答案 1 :(得分:1)

老实说,我认为使用三元运算符不会使代码更好。 我建议您尝试通过创建易于查找的数据结构来减少if-else链,如下所示:


const whatBeats = {
  'scissors': 'rock',
  'paper': 'scissors',
  'rock': 'paper'
};
const compareHands = (playerChoice, computerChoice) => {
  // Update Text
  const winner = document.querySelector('.winner');
  const winnerIs = (who, isPlayerWin) => {

    winner.textContent = `${who} Wins!`;

    isPlayerWin ? pScore++ : cScore++;

    updateScore();
  };

  // Check for tie
  if (playerChoice === computerChoice) {
    winner.textContent = 'It Is A Tie!';
    // Check For Winner
  } else if (playerChoice === whatBeats[computerChoice]) {
    winnerIs('Player', true);
  } else {
    winnerIs('Computer', false)
  }
}

在这种情况下,我们将游戏动态视为数据,将其集中在一个地方。

对于下一个问题,请尝试解决之前的问题(有大量有关三元运算符的教程)。

答案 2 :(得分:0)

尝试这样。尽管可读性是一个问题,因为嵌套的object storage过多,已被三元运算符代替。将if elsetrue分别替换为false!0以缩短语句

!1

答案 3 :(得分:0)

使用太多的三进制可能导致代码无法读取。我建议您可以将切换用例与三元运算符结合使用。

switch (playerChoice) {
   case computerChoice: winner.textContent = 'It Is A Tie!';break;
   case 'rock': computerChoice === 'scissors' ?  winnerIs('Player', true) : winnerIs('Computer', false); break;
   case 'paper': computerChoice === 'rock' ?  winnerIs('Player', true) : winnerIs('Computer', false); break;
   case 'scissors': computerChoice === 'paper' ?  winnerIs('Player', true) : winnerIs('Computer', false); break;
    }

答案 4 :(得分:0)

如果您需要更多DRYied代码。您可以尝试使用此解决方案来避免多次调用winnerIs函数。

const compareHands = (playerChoice, computerChoice) => {
  const winner = document.querySelector('.winner');

  if (playerChoice == computerChoice) {
    winner.textContent = 'It Is A Tie!';
    return;
  }

  let choices = ['rock', 'paper', 'scissor'];
  let playerIndex, computerIndex, isPlayerWin;

  playerIndex = choices.indexOf(playerChoice);
  choices.push(choices.shift());
  computerIndex = choices.indexOf(computerChoice);
  isPlayerWin = playerIndex !== computerIndex;

  winner.textContent = `${isPlayerWin ? 'Player' : 'Computer'} Wins!`;
  isPlayerWin ? pScore++ : cScore++;
  updateScore();
}