Javascript Rock Paper Scissors Challenge


#1

Can somebody help me with my code? When I run it, nothing is being logged into the console.

The challenge:

Rock, Paper, or Scissors

Rock paper scissors is a classic two player 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:

  • Rock destroys scissors.
  • Scissors cut paper.
  • Paper covers rock.
  • If there’s a tie, then the game ends in a draw.

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.

My code:

const getUserChoice = userInput =>{
  userInput = userInput.toLowerCase();
  if (userInput === 'rock' || userInput==='paper' || userInput ==='scissors') {
    return userInput;
  } else {
    console.log('not a valid choice');
  }
};

function getComputerChoice() {
  switch(Math.floor(Math.random()*3)) {
    case 0:
      return 'rock';
      break;
    case 1:
      return 'scissors';
      break;
    case 2:
      return 'paper';
      break;
  	};
}

function determineWinner(userChoice,computerChoice) {
  if (userChoice === computerChoice) {
    return 'It\'s a tie!';
  } else if (userChoice === 'rock') {
    if (computerChoice === 'paper') {
      return 'Computer wins!';
    } else {
      return 'You win!';
    }
  } else if (userChoice === 'paper'){
    if (compterChoice === 'scissors') {
      return 'Computer wins!';
    }else {
      return 'You win!';
    }
  } else if (userChoice === 'scissors') {
    if (computerChoice === 'rock') {
      return 'Computer wins!';
    } else {
      return 'You win!';
    }
  } else if (userChoice === 'bomb') {
    return 'You win!';
  }
};

function playGame() {
  var userChoice = getUserChoice('scissors');
  var computerChoice = getComputerChoice()
  console.log(`you threw ${userChoice}`);
  console.log(`computer threw ${computerChoice}`);
  console.log(determineWinner(userChoice,computerChoice));
};








#2

Have you called the playGame() function?


#3

Oops! It’s working now, thanks! :grin:


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.