Rock-paper-scissors-javascript

Hi, all!

const getUserChoice = userInput => {
userInput = userInput.toLowerCase();

if (userInput === 'rock' || userInput === 'scissors' || userInput === 'paper') {
  return userInput;
} else {
  console.log( "Error! Try again" );
}
}
//3,4
const getComputerChoice = () => {
  const randomNumber = Math.floor(Math.random() * 3);
  
  switch (randomNumber) {
    case 0 :
    return 'rock';
    case 1 :
    return "paper";
    case 2 : 
    return "scissors";
  }
};


const determineWinner = (userChoice , computerChoice) => {
   if (userChoice === computerChoice) {
     return 'Its a tie';}
   
  if (userChoice === 'rock') {
    if (computerChoice === 'paper') {
      return 'Computer WON'
    } else {
      return 'You WON!'
    };
    }

if (userChoice === 'paper') {
  if(computerChoice === 'scissors') {
    return 'Computer WON'
  } else {
    return "You WON!"
  }
}

if (userChoice === 'scissors') {
  if (computerChoice === 'rock')
  return "The computer WON!" 
} else {
  return "You won!";
};
}



const playGame = () => {
  const userChoice = getUserChoice('scissors');
  const computerChoice = getComputerChoice() ;
  console.log('You threw: ' + userChoice);
  console.log('The computer threw: ' + computerChoice);

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


playGame();


Please help me with this code, all working except scissors, result - undefined.
Appreciate your time and help!

Hello @nataliiadovgal126765,

Thank you for reaching out.

It looks like there is a problem with your if statements.

Let’s work through this:

const determineWinner = (userChoice , computerChoice) => {
  if (userChoice === computerChoice) {
    return 'Its a tie';}
  
 if (userChoice === 'rock') {
   if (computerChoice === 'paper') {
     return 'Computer WON'
   } else {
     return 'You WON!'
   };
   }

if (userChoice === 'paper') {
 if(computerChoice === 'scissors') {
   return 'Computer WON'
 } else {
   return "You WON!"
 }
}

if (userChoice === 'scissors') {
 if (computerChoice === 'rock')
 return "The computer WON!" 
} else {
 return "You won!";
};
}

The format of an if-else-if-else block is this:

if (x === y) {
  return true;
} else if (x !== y) {
  return false;
} else {
  return undefined;
}

Refactoring your code we get:

if (userChoice === computerChoice) {
  return "It's a tie";
} else if (userChoice === 'rock') {
  if (computerChoice === 'paper') {
    return 'Computer Won';
  } else {
    return 'You won';
 }
} else if (userChoice === 'paper') {
   if(computerChoice === 'scissors') {
     return 'Computer Won';
   } else {
     return 'You won';
  }
//The following if statemement could be replaced with else such as
// } else {
//
// }
} else if (userChoice === 'scissors') {
   if (computerChoice === 'rock') {
     return 'Computer Won';
   } else {
     return 'You won';
  }
}

This is why it is important to keep code simple, however, I understand that you are following the class.

In addition, please ensure you use semicolons where and when required. This can sometimes cause errors and may prevent your program from even running in certain languages.

Let me know if this works.
Happy coding,

Richard.

1 Like

Hello~ @digital0457018858![Screenshot 2020-05-13 at 11.16.06 AM|683x499, 50%]
Thanks for your fast reply!
I see, yes, makes sense !it works now.
But I dont understand, how it works in the video instruction then. https://youtu.be/cKbN6-1ZQ3A I attach screenshot as well!

1 Like

There are soooo many ways to compute this game…

https://repl.it/@mtf/RPS-experimental

RPSB (Esc=Quit)> bomb

      Computer: paper
      Person:   bomb
      Outcome:  Person wins by default
      
RPSB (Esc=Quit)> robk
Invalid input
RPSB (Esc=Quit)> paper

      Computer: paper
      Person:   paper
      Outcome:  The result is a tie

RPSB (Esc=Quit)> scissors

      Computer: rock
      Person:   scissors
      Outcome:  Computer Wins
      
RPSB (Esc=Quit)> paper

      Computer: rock
      Person:   paper
      Outcome:  Person Wins
      
RPSB (Esc=Quit)> 

This code is buggy. What it needs is a good mind to sort that out.

1 Like

You are most welcome @nataliiadovgal126765.

As @mtf states, there are many different ways for this game to be computed, would I use the way outlined in the tutorial? Probably not, as we all have our different coding styles. And, in a production
environment you would be expected to follow a “house style” and follow the plans as dictated by the product team.

Happy coding.
Best wishes,

Richard.

1 Like