Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

217
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!