Javascript if{}else if{if{}else{}的三元条件
我想把所有if-else语句都改为三元运算符。if-else语句的三元运算符是什么Javascript if{}else if{if{}else{}的三元条件,javascript,conditional-statements,ternary,Javascript,Conditional Statements,Ternary,我想把所有if-else语句都改为三元运算符。if-else语句的三元运算符是什么 const compareHands = (playerChoice, computerChoice) => { // Update Text const winner = document.querySelector('.winner'); const winnerIs = (who, isPlayerWin) => {
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太多,被三元运算符替换。真与假被替换为!0 & !1分别缩短语句 playerChoice==计算机选择? winner.textContent=这是一场平局!: 摇滚乐===玩家选择? 剪刀===计算机选择? winnerIsPlayer!0 : 温尼里斯计算机!1 : 纸张===玩家选择? 剪刀===计算机选择? 温尼里斯计算机!1 : winnerIsPlayer!0 : 剪刀===玩家选择和摇滚===电脑选择?温尼里斯计算机!1 :
winnerIsPlayer!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;
}
如果你想要一个更加枯燥的代码。您可以尝试此解决方案以避免多次调用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();
}
我不会使用条件运算符或if-else-我更喜欢使用数组,并比较指示符尝试查看此项@总有比最好的更好的方法。我刚刚回答了SO用户提出的问题。如果..我可以建议他使用对象映射而不是嵌套。。其他人已经做了,但这不是他的答案question@brk是的,我的意思不是批评——你确实回答了OP的问题,只是没有人应该在生产中真正做到这一点: