Code Issue - Help Rock Paper and Scissors

Hi team,

Can someone please check and tell if my code is correct?
There is no issue with the syntax but output does not match intended goal.

Thanks,
Aditya

code is below

const getUserChoice = userInput => {

userInput = userInput.toLowerCase();

if(userInput === 'rock' || userInput === 'paper'|| userInput === 'scissors' ) 
{
  return userInput;

}

else {
console.log("error");}

};
console.log("User choice is " +  getUserChoice('Rock'));


function getComputerChoice(){

var randomNum = Math.floor(Math.random() * 3);

switch(randomNum){

case 0:
  return 'rock';
  break;

case 1:
  return 'paper';
  break;

case 2:
  return 'scissors';
  break;

  default:
      alert( "I don't know such values" );
};


};
console.log("Computer chose " +  getComputerChoice());

function determineWinner(userChoice, computerChoice){

if(userChoice === computerChoice){
  return 'The game is a tie!';
}

if (userChoice === 'rock') {
  if (computerChoice === 'paper') {
    return 'The computer won! and chose ' + computerChoice;
  } else {
    return 'User won because chose '  + userChoice;
  }
}

if (userChoice === 'paper') {
  if (computerChoice === 'scissors') {
    return 'The computer won! and chose ' + computerChoice;
  } else {
    return 'User won because chose ' + userChoice;
  }
}

if(userChoice === 'scissors'){
  if(computerChoice ==='paper'){
    return 'User won and  chose ' + userChoice ;
}
    else {
      return 'The computer won! and chose ' + computerChoice;
}

}

}

console.log("Winner now is " + determineWinner('paper', 'rock'));



function playGame(){

const userChoice = getUserChoice('rock');

const computerChoice = getComputerChoice();

console.log(" You chose " + userChoice);

console.log("Computer chose " + computerChoice);

console.log(determineWinner(userChoice,computerChoice));

playGame();

};

All the calls to getUserChoice, getComputerChoice and determineWinner should now be made from playGame function. Which you do, but you also still have a couple of old function calls to these function. Those should be removed

finally, playGame should be called correctly. Currently, the call to playGame is within the body of the playGame function itself.

Hi,

Thanks for the feedback. Can you please point where i am calling old functions?

I do understand the playGame should be called outside that function.

Thanks,
Aditya

well, any call to getUserChoice, getComputerChoice and determineWinner which is not part of playGame function

1 Like