Got stuck with rock, paper, scissors game


#1

Hi guys. I’ve just started my story with programming and got stuck at this point in the course. Could anyone explain it to me? Thanks.

userInput = 'Rock';
userInput = userInput.toLowerCase();

function getComputerChoice() {
  Math.floor(Math.random() * 3);

  switch (randomNumber) {
    case 0:
      return 'rock';
    case 1:
      return 'paper';
    case 2:
      return 'scissors';
  }
}

function determineWinner(userChoice, computerChoice) {
  if (userChoice === computerChoice) {
    return 'The game was tie';
  }
  if (userChoice === 'rock'){
    if (computerChoice === 'paper') {
      return 'The computer won!';
    } else {
      return 'You won';
    }
  }

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

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

}

function playGame() {
  var userChoice = userInput;
  var computerChoice = getComputerChoice();
  console.log('You threw: ' + userChoice);
  console.log('The computer threw: ' + computerChoice);

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

playGame();

#2

here:

switch (randomNumber)

you get an error:

ReferenceError: randomNumber is not define

you try to use a variable which doesn’t exist (isn’t defined), that will give an error


#3

Thank you very much! I put Math.floor object into a variable named randomNumber and it works.


#4

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