I'm currently write the litte Scissors, paper, rock game and I used "If statement" to do so. Now, I try to write it with the "switch statement". So I will show you what I wrote but nothing happen in my console and I don't know why. Seems pretty easy for anybody used to code everyday I think.
So I begin to declare my variables.
let rock = 'rock';
let scissors = 'scissors';
let paper = 'paper';
then these two variables will be changed to test the conditional
let firstPlayerChoice = paper;
let secondPlayerChoice = rock;
After that I made my seven possibilities
let firstPossibility = firstPlayerChoice === paper && secondPlayerChoice === rock;
let secondPossibility = firstPlayerChoice === scissors && secondPlayerChoice === paper;
let thirdPossibility = firstPlayerChoice === rock && secondPlayerChoice === scissors;
let fourthPossibility = firstPlayerChoice === rock && secondPlayerChoice === paper;
let fifthPossibility = firstPlayerChoice === paper && secondPlayerChoice === scissors;
let sixthPossibility = firstPlayerChoice === scissors && secondPlayerChoice === rock;
let seventhPossibility = firstPlayerChoice === secondPlayerChoice;
So, now I create my condition using my Switch statement but nothing is running on my console. The fact is I have difficulties to identify the good expression.
switch (firstPlayerChoice) {
case firstPlayerChoice === paper && secondPlayerChoice === rock:
case firstPlayerChoice === scissors && secondPlayerChoice === paper:
case firstPlayerChoice === rock && secondPlayerChoice === scissors:
console.log('First Player won !');
break;
}
switch (secondPlayerChoice) {
case fourthPossibility:
case fifthPossibility:
case sixthPossibility:
console.log('Second player won !');
break;
}
Someone can provide me just one expression to help to understand the big picture please ?