Ad
Code
Diff
  • function dumbRockPaperScissors(player1, player2) {
    player1 = player1.toLowerCase();
      player2 = player2.toLowerCase();
      const logic = {
        "rock": ["scissors"],
        "paper": ["rock"],
        "scissors": ["paper"]
      };
      
        if (!logic[player1] || !logic[player2]) {
        return "Invalid input! Both players must choose 'rock', 'paper', or 'scissors'.";
      }
    
      if(logic[player1].includes(player2)){
        return "Player 1 wins"
      }else if(logic[player2].includes(player1)){
        return "Player 2 wins"
      }else{
        return "Draw"
      }
      
    }
    • function dumbRockPaperScissors(player1, player2) {
    • if(player1 == "Rock" && player2 == "Paper"){
    • return "Player 2 wins";
    • }
    • else if(player1 == "Rock" && player2 == "Scissors" ){
    • return "Player 1 wins";
    • }
    • else if(player1 == "Scissors" && player2 == "Paper"){
    • return "Player 1 wins";
    • }
    • else if(player1 == "Scissors" && player2 == "Rock"){
    • return "Player 2 wins";
    • }
    • else if(player1 == "Paper" && player2 == "Scissors"){
    • return "Player 2 wins";
    • }
    • else if(player1 == "Paper" && player2 == "Rock"){
    • return "Player 1 wins";
    • }
    • else if(player1 == "Paper" && player2 == "Paper"){
    • return "Draw";
    • }
    • else if(player1 == "Rock" && player2 == "Rock"){
    • return "Draw";
    • }
    • else if(player1 == "Scissors" && player2 == "Scissors"){
    • return "Draw";
    • player1 = player1.toLowerCase();
    • player2 = player2.toLowerCase();
    • const logic = {
    • "rock": ["scissors"],
    • "paper": ["rock"],
    • "scissors": ["paper"]
    • };
    • if (!logic[player1] || !logic[player2]) {
    • return "Invalid input! Both players must choose 'rock', 'paper', or 'scissors'.";
    • }
    • if(logic[player1].includes(player2)){
    • return "Player 1 wins"
    • }else if(logic[player2].includes(player1)){
    • return "Player 2 wins"
    • }else{
    • return "Draw"
    • }
    • }
Code
Diff
  • const getUserChoice = (userInput) => {
      const formattedInput = userInput.toLowerCase(); 
    
     
    
      if (formattedInput === 'rock' || formattedInput === 'paper' || formattedInput === 'scissors') {
        return formattedInput;
      } else {
        console.log('Error!');
      }
    };
    
    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 'It\'s a tie!';
      }
      if (userChoice === 'rock') {
        if (computerChoice === 'paper') {
          return 'Computer wins!';
        } else {
          return 'You win!';
        }
      }
    
      if (userChoice === 'paper') {
        if (computerChoice === 'rock') {
          return 'You win!';
        } else {
          return 'Computer wins!';
        }
      }
    
      if (userChoice === 'scissors') {
        if (computerChoice === 'rock') {
          return 'Computer wins!';
        } else {
          return 'You win!';
        }
      }
    };
    
      
    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();
    
    • function rps(player1, player2) {
    • return player1 == player2 ? 'Draw!' :
    • player1 == 'rock' && player2 == 'scissors' ||
    • player1 == 'scissors' && player2 == 'paper' ||
    • player1 == 'paper' && player2 == 'rock' ? 'Player 1 won!' : 'Player 2 won!';
    • }
    • const getUserChoice = (userInput) => {
    • const formattedInput = userInput.toLowerCase();
    • if (formattedInput === 'rock' || formattedInput === 'paper' || formattedInput === 'scissors') {
    • return formattedInput;
    • } else {
    • console.log('Error!');
    • }
    • };
    • 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 'It\'s a tie!';
    • }
    • if (userChoice === 'rock') {
    • if (computerChoice === 'paper') {
    • return 'Computer wins!';
    • } else {
    • return 'You win!';
    • }
    • }
    • if (userChoice === 'paper') {
    • if (computerChoice === 'rock') {
    • return 'You win!';
    • } else {
    • return 'Computer wins!';
    • }
    • }
    • if (userChoice === 'scissors') {
    • if (computerChoice === 'rock') {
    • return 'Computer wins!';
    • } else {
    • return 'You win!';
    • }
    • }
    • };
    • 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();