简体   繁体   中英

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;
        }
    } 
}

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM