我想将所有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);
}
}
}
试试这样吧。虽然可读性是一个问题,因为有太多的嵌套if else
被三元运算符取代。 true
和false
分别用!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));
正如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
}
老实说,我不认为使用三元运算符会使代码更好。我建议您尝试通过创建一个易于查找的数据结构来减少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)
}
}
在这种情况下,我们将游戏动态视为数据,将其集中在一个地方。
对于下一个问题,尝试解决问题之前(有大量关于三元运算符的教程)。
使用太多的三元组可能导致无法读取的代码。我建议你可以使用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;
}
如果你想要更多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();
}