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

问题描述 投票:-3回答:5

我想将所有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);
            }
        }
    }
javascript condition ternary
5个回答
0
投票

试试这样吧。虽然可读性是一个问题,因为有太多的嵌套if else被三元运算符取代。 truefalse分别用!0!1取代缩短声明

playerChoice === computerChoice ?
  winner.textContent = "It Is A Tie!" :
  "rock" === playerChoice ?
  "scissors" === computerChoice ?
  winnerIs("Player", !0) :
  winnerIs("Computer", !1) :
  "paper" === playerChoice ?
  "scissors" === computerChoice ?
  winnerIs("Computer", !1) :
  winnerIs("Player", !0) :
  "scissors" === playerChoice && ("rock" === computerChoice ? winnerIs("Computer", !1) :
    winnerIs("Player", !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
投票

老实说,我不认为使用三元运算符会使代码更好。我建议您尝试通过创建一个易于查找的数据结构来减少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)
  }
}

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

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


0
投票

使用太多的三元组可能导致无法读取的代码。我建议你可以使用switch case结合三元运算符。

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;
    }

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();
}
© www.soinside.com 2019 - 2024. All rights reserved.