Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

140
Visualizações
Alternative of "if statement" with "switch statement

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 ?

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda