Rock, Paper, or Scissors

https://www.codecademy.com/courses/introduction-to-javascript/projects/rock-paper-scissors-javascript

I am trying to do Rock, PaperI, or Scissors this project in JavaScript lesson.

below is my code ,I am trying to switch /case , instead of if/else. but get running result as below which it’s wrong and not follow my code logic. can anyone explain to me why my code doesn’t work ?
user chooses paper
paper
computer is winner

const getUserChoice = userInput =>{
  userInput = userInput.toLowerCase();
  if(userInput==='rock' || userInput ==='paper'|| userInput==='scissors'){
    return userInput;
  }else {
    console.log('Error!');
  } 
};

//console.log(getUserChoice('Rock'));

const getComputerChoice = () => {
  let number = Math.floor(Math.random()*3);
  switch(number){
    case 0:
      return 'rock';
      break;
    case 1:
      return 'paper';
      break;
    case 2 : 
      return 'scissors';
      break;
  default :
      console.log('Error!');    
  };
  
};
//console.log(getComputerChoice());

const determineWinner = function (userChoice,computerChoice) {
 
  switch(userChoice ==='rock'){
    case computerChoice ==='paper':
      return 'computer is winner';
      break;
    case computerChoice ==='scissors':
      return 'user is winner';
      break;  
    case computerChoice ==='rock':
      return 'game is tie';
      break;  
  };
  switch(userChoice === 'paper'){
      case computerChoice ==='rock':
        return 'user is winner';
        break;
      case computerChoice ==='scissors' :
        return 'computer is winner';
        break;   
      case computerChoice ==='paper':
        return 'game is tie';
        break;  
  };
   switch(userChoice ==='scissors'){
      case computerChoice ==='paper':
      return 'user is winner';
      break;
    case computerChoice ==='rock':
      return 'computer is winner';
      break; 
    case computerChoice ==='scissors':
      return 'game is tie';
      break; 
  };
    
}
console.log('user chooses paper');
console.log(getComputerChoice());
console.log(determineWinner ('paper', getComputerChoice()));
```

take a look at the switch documentation:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch

now, a switch means you take a variable (for example userChoice and put it in a switch statement:

switch(userChoice){
  //
}

then the cases could be be 'rock' for example:

case 'rock':

you don’t need any comparisons, switch is doing that for you

Thank you
the I modified my code as below which it’s same as video provided , but still get running result like this which make me very confuse. I can’t find what’s wrong with my code. can you help me to check it ? thank you very much

const getUserChoice = userInput =>{
  userInput = userInput.toLowerCase();
  if(userInput==='rock' || userInput ==='paper'|| userInput==='scissors'){
    return userInput;
  }else {
    console.log('Error!');
  } 
};
//console.log(getUserChoice('Rock'));
const getComputerChoice = () => {
  let number = Math.floor(Math.random()*3);
  switch(number){
    case 0:
      return 'rock';
      break;
    case 1:
      return 'paper';
      break;
    case 2 : 
      return 'scissors';
      break;
  default :
      console.log('Error!');    
  };
  
};
//console.log(getComputerChoice());
const determineWinner = function (userChoice,computerChoice) {
  if (userChoice === 'bomb'){
    return 'Use wooooon!!!';
  } 
  if (userChoice === computerChoice){
    return ' game is tie';
  } 
  if (userChoice ==='rock'){
    if(computerChoice ==='paper'){
      return 'computer won';
    }else {
      return 'user won';
    }
  }
  if( userChoice ==='paper'){
    if(computerChoice ==='rock'){
      return 'user won';
    }else {
      return 'computer won';
    }
  } 
  if( userChoice ==='scissors'){
    if(computerChoice ==='paper'){
      return 'user won';
    }else {
      return 'computer won';
    } 
  }
    
};

const playGame = () =>{
const userChoice = getUserChoice('paper');
const computerChoice = getComputerChoice() ;
console.log('you chooose :'+ userChoice);
console.log('computer chooose : ' + getComputerChoice());
console.log(determineWinner(userChoice, computerChoice));
};

playGame();

running result :
you chooose :paper
computer chooose : scissors
game is tie

here:

console.log('computer chooose : ' + getComputerChoice());

you call getComputerChoice again, which will generate a new random choice for the computer. So what is logged to the console, will very likely not match with what the computer picked the first time.

use the already randomly generated choice which you stored in computerChoice variable

1 Like