Reputation: 12129
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
}
};
My current attempt is:
function changePlayer(){
currentPlayer === playerOne ? playerTwo : playerOne;
}
Upvotes: 0
Views: 90
Reputation: 969
You just miss the assignment statement. So the final example will go like this:
function changePlayer(){
currentPlayer = (currentPlayer === playerOne) ? playerTwo : playerOne;
}
Upvotes: 2
Reputation: 2377
The first argument of the ternary operator is the condition:
function changePlayer(){
currentPlayer = (currentPlayer === playerOne) ? playerTwo : playerOne;
}
Upvotes: 1