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

224
Visualizações
Why is my code executing on every switch case instead of breaking?

Code is meant to take two arguments that I type in when I call playRound in the console. When I put in 'rock' as both playerSelection and computerSelection it executes all code under playerSelection case 'rock' eg- You both chose rock It's a draw! You chose rock and the CPU chose rock. . . You win! You chose rock and the CPU chose rock. . . You lose!

function playRound(playerSelection, computerSelection) {
// LINE BELOW IS COMMENTED OUT FOR TESTING
// playerSelection = window.prompt("Rock, paper, or scissors?")

// Conditional statement to have different outcomes depending on player selection
switch (playerSelection) {
    case 'rock':
        // conditional outcomes depending on computer selection after player selects rock
        switch (computerSelection) {
            case 'rock':
                console.log(`You both chose ${playerSelection} 
                It's a draw!`);
                break;
            case 'paper':
                console.log(`You chose ${playerSelection} and the CPU chose ${computerSelection}. . .
                You lose!`);
                break;
            case 'scissors':
                console.log(`You chose ${playerSelection} and the CPU chose ${computerSelection}. . .
                You win!`);
                break;
        }
        // condition outcomes if player selects paper   
        case 'paper':
            // conditional outcomes depending on computer selection after player selects rock
            switch (computerSelection) {
                case 'paper':
                    console.log(`You both chose ${playerSelection} 
                    It's a draw!`);
                    break;
                case 'scissors':
                    console.log(`You chose ${playerSelection} and the CPU chose ${computerSelection}. . .
                    You lose!`);
                    break;
                case 'rock':
                    console.log(`You chose ${playerSelection} and the CPU chose ${computerSelection}. . .
                    You win!`);
                    break;    
        }
        case 'scissors':
            // conditional outcomes depending on computer selection after player selects scissors
            switch (computerSelection) {
                case 'scissors':
                    console.log(`You both chose ${playerSelection} 
                    It's a draw!`);
                    break;
                case 'rock':
                    console.log(`You chose ${playerSelection} and the CPU chose ${computerSelection}. . .
                    You lose!`);
                    break;
                case 'paper':
                    console.log(`You chose ${playerSelection} and the CPU chose ${computerSelection}. . .
                    You win!`);
                    break;
        }
    } 
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You don't have a break in the outer most switch statement.

switch (playerSelection) {
    case 'rock':
        // conditional outcomes depending on computer selection after player selects rock
        switch (computerSelection) {
            case 'rock':
                console.log(`You both chose ${playerSelection} 
                It's a draw!`);
                break;
            case 'paper':
                console.log(`You chose ${playerSelection} and the CPU chose ${computerSelection}. . .
                You lose!`);
                break;
            case 'scissors':
                console.log(`You chose ${playerSelection} and the CPU chose ${computerSelection}. . .
                You win!`);
                break;
        }
break;

about 4 years ago · Juan Pablo Isaza Relatório
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