How do I convert this [ else if ] statement to [ Switch ] statement!

I’m trying to convert this if Else if statement to a switch statement

I have tried using other options in the switch statement’s (key) but can’t find an option…

// Using  if Else if statements...
 /*
  if (userChoice === 'rock' && computerChoice === 'paper') {
    return 'The Computer Won!';
  } else if (userChoice === 'paper' && computerChoice === 'rock'){ 
     return 'The User Won!';
   } else if (userChoice === 'scissors' && computerChoice === 'rock'){
     return 'The Computer Won!';
   } else if (userChoice === 'rock' && computerChoice === 'scissors'){
     return 'The User Won!';
   } else if (userChoice === 'scissors' && computerChoice === 'paper'){
     return 'The User Won!';
   } else if (userChoice === 'paper' && computerChoice === 'scissors'){
     return 'The Uomputer Won!'; 
   } else if (userChoice === computerChoice){
     return 'Match Tied';
   } else if (userChoice === 'bomb'){
     return 'The User Won by using a secret weapon!!'};*/
};

// Trying to use switch statement, which is not working...

const determineWinner = (userChoice, computerChoice) => {

  switch (userChoice, computerChoice) {
    case userChoice === computerChoice:
      return 'We have ourselves a Tie!';
      break;
    case userChoice === 'rock' && computerChoice === 'paper':
      return 'The Computer Won!';
      break;
    case userChoice === 'rock' && computerChoice === 'scissors':
      return 'The User Won!';
      break;
    case userChoice === 'paper' && computerChoice === 'scissors':
      return 'The Computer Won!';
      break;
    case userChoice === 'paper' && computerChoice === 'rock':
      return 'The User Won!';
      break;
    case userChoice === 'scissors' && computerChoice === 'rock':
      return 'The Computer Won!';
      break;
    case userChoice === 'scissors' && computerChoice === 'paper':
      return 'The User Won!';
      break;
    case userChoice === 'bomb':
      return 'The User Played a Higher Card!';
      break;
  }
};
*/

it looks like your switch syntax isn’t correct. Take a look at the examples here.

Notice that what is between parenthesis after the keyword switch is the expression being evaluated. And what you place after keyword case is the result of that expression.

1 Like

here’s an example to get you started thinking in a possible solution:

switch (userChoice) {
   case 'rock':
      if computerChoice === 'paper':
         return 'The Computer Won!'
}
1 Like

Two (inelegant but they work) ways of implementing switch version:

Approach A:

const determineWinner = (userChoice, computerChoice) => {
    
    if (userChoice === computerChoice) {
        return 'We have ourselves a Tie!';
    }
    
    switch (userChoice) {
        
        case 'rock':
            switch (computerChoice) {
                case 'paper':
                    return 'The Computer Won!';
                case 'scissors':
                    return 'The User Won!';
            }
            
        case 'paper':
            switch (computerChoice) {
                case 'rock':
                    return 'The User Won!';
                case 'scissors':
                    return 'The Computer Won!';
            }
            
        case 'scissors':
            switch (computerChoice) {
                case 'rock':
                    return 'The Computer Won!';
                case 'paper':
                    return 'The User Won!';
                    
            }
            
        case 'bomb':
            return 'The User Played a Higher Card!';
    }

};

Approach B:

const determineWinner = (userChoice, computerChoice) => {
    
    switch (true) {
        case (userChoice === computerChoice):
            return 'We have ourselves a Tie!';
            
        case ((userChoice === 'rock') && (computerChoice === 'paper')):
            return 'The Computer Won!';
            
        case ((userChoice === 'rock' ) && (computerChoice === 'scissors')):
            return 'The User Won!';
            
        case ((userChoice === 'paper' ) && (computerChoice === 'rock')):
            return 'The User Won!';
            
        case ((userChoice === 'paper' ) && (computerChoice === 'scissors')):
            return 'The Computer Won!';
            
        case ((userChoice === 'scissors') && (computerChoice === 'rock')):
            return 'The Computer Won!';
            
        case ((userChoice === 'scissors') && (computerChoice === 'paper')):
            return 'The User Won!';
            
        case (userChoice === 'bomb'):
            return 'The User Played a Higher Card!';
    }
    
}
1 Like

Thanks for your time dude!
Got it…

Thanks man for your time

1 Like