Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const getUserChoice = (userInput) => {
- userInput = userInput.toLowerCase();
- if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissors') {
- return userInput;
- } else {
- console.log('error');
- }
- };
- const getComputerChoice = () => {
- let randomNumber = Math.random();
- if (randomNumber < 0.33) {
- return 'rock';
- } else if (randomNumber > 0.34 && randomNumber < 0.66) {
- return 'paper';
- } else if (randomNumber > 0.67) {
- return 'scissors';
- }
- };
- const determineWinner = (userChoice, computerChoice) => {
- if (userChoice === computerChoice) {
- return 'tie';
- }
- if (userChoice === 'rock') {
- if (computerChoice === 'scissors') {
- return 'user won';
- } else {
- return 'computer 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('scissors');
- const computerChoice = getComputerChoice();
- console.log('user threw: ' + userChoice);
- console.log('computer threw: ' + computerChoice);
- console.log(determineWinner(userChoice, computerChoice));
- };
- playGame();
Add Comment
Please, Sign In to add comment