Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

227
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda