Nalini1998 / Project_Public

MIT License
2 stars 0 forks source link

Rock, Paper, or Scissors Project #503

Closed Nalini1998 closed 1 year ago

Nalini1998 commented 1 year ago

Reference code was put at the comment below

Rock paper scissors Project is a classic two players game. Each player chooses either rock, paper, or scissors. The items are compared, and whichever player chooses the more powerful item wins.

The possible outcomes are:

Our code will break the game into four parts:

  1. Get the user’s choice.
  2. Get the computer’s choice.
  3. Compare the two choices and determine a winner.
  4. Start the program and display the results.
Nalini1998 commented 1 year ago
// @Meow.Nalini98

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

// console.log(getUserChoice('scissors'))

const getComputerChoice = () => {
  const randomNumber = Math.floor(Math.random()*3);
  switch(randomNumber) {
    case 0:
      return "rock"
    case 1:
      return "paper"
    case 2:
      return "scissors"
  }
};

// console.log(getComputerChoice())

const determineWinner = (userChoice, computerChoice) => {
  if (userChoice === computerChoice) {
    return 'The game is a tie!';
  }
  if (userChoice === 'rock') {
    if (computerChoice === 'paper') {
      return "That's a pity! The computer won!"
    } else {
      return "Congrats! You won!"
    } 
  } 

  if (userChoice === 'paper') {
    if (computerChoice === 'scissors') {
       return "That's a pity! The computer won!" 
    } else {
      return "Congrats! You won!"
    }
  }

  if (userChoice === 'scissors') {
    if (computerChoice === 'rock') {
       return "That's a pity! The computer won!" 
    } else {
      return "Congrats! You won!"
    }
  }
}

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

playGame()