本文介绍了javascript if / else反对的三元运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用javascript的三元运算符语法编写此if / else语句。
I am trying to write this if/else statement using javascript's ternary operator syntax. Is it possible to write this as a ternary operator?
function changePlayer() {
if (currentPlayer === playerOne) {
currentPlayer = playerTwo
} else {
currentPlayer = playerOne
}
};
我当前的尝试是:
function changePlayer(){
currentPlayer === playerOne ? playerTwo : playerOne;
}
推荐答案
您只是错过了赋值语句。因此,最后一个示例将如下所示:
You just miss the assignment statement. So the final example will go like this:
function changePlayer(){
currentPlayer = (currentPlayer === playerOne) ? playerTwo : playerOne;
}
这篇关于javascript if / else反对的三元运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!