Project. Rock, paper or scissors

Hi.
I am pretty new in programming. I try to get this code to work but ‘userChoice’ just get ‘undefined’ and I don´t understand why.
Can anyone help me?

const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();
  if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissor') {
    return userInput;
  } else {
    console.log('Error!');  
  } 
 };
/*
I´m selecting bettwen 3 different things. Rock, paper or scissors. These 3 different things is eaqual to (getUserChoice). 
*/
 function getComputerChoice() {
 const getComputerChoice = Math.floor(Math.random() * 3); 
  switch (getComputerChoice) {
    case 0:
      return 'rock';
    case 1:
      return 'paper';
    case 2:
      return 'scissor'; 
 }   
};
//The computer choose randomly bettwen rock, paper or scissor:

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

    if (userChoice === 'scissor') {
      if (computerChoice === 'rock') {
        return 'The computer won!';
      } else {
        return 'You won!';
      }
    };
    if (userChoice === 'bomb'){
      return 'You won it all!';
    };
};   

const playGame = () => {
  const userChoice = getUserChoice(' paper');
  const computerChoice = getComputerChoice();
  console.log('You threw: ' +userChoice);
  
};
playGame();
const getUserChoice = userInput => {
  userInput = userInput.toLowerCase();
  if (userInput === 'rock' || userInput === 'paper' || userInput === 'scissor') {
    return userInput;
  } else {
    console.log('Error!');  
  } 
 };
/*
I´m selecting bettwen 3 different things. Rock, paper or scissors. These 3 different things is eaqual to (getUserChoice). 
*/
 function getComputerChoice() {
 const getComputerChoice = Math.floor(Math.random() * 3); 
  switch (getComputerChoice) {
    case 0:
      return 'rock';
    case 1:
      return 'paper';
    case 2:
      return 'scissor'; 
 }   
};
//The computer choose randomly bettwen rock, paper or scissor:

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

    if (userChoice === 'scissor') {
      if (computerChoice === 'rock') {
        return 'The computer won!';
      } else {
        return 'You won!';
      }
    };
    if (userChoice === 'bomb'){
      return 'You won it all!';
    };
};   

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

when you get in the else clause:

  } else {
    console.log('Error!');  
  } 

you only log an error string, you don’t return anything, so you get undefined.

Thanks for the help.

const getUserChoice = userInput => {

userInput = userInput.toLowerCase();

if (userInput === ‘rock’ || userInput === ‘paper’ || userInput === ‘scissor’) {

return userInput;

} else {

console.log('Error!');  

}

};

function getComputerChoice() {

const getComputerChoice = Math.floor(Math.random() * 3);

switch (getComputerChoice) {

case 0:

  return 'rock';

case 1:

  return 'paper';

default:

  return 'scissor'; 

}

};

function determineWinner(userChoice, computerChoice) {

if (userChoice === computerChoice) {

return 'The game is a tie!';

};

if (userChoice === ‘rock’) {

if (computerChoice === ‘paper’) {

return 'The computer won!';

} else {

return 'You won!';

};

};

if (userChoice === ‘paper’) {

if (computerChoice === 'scissor') {

  return 'The computer won!';

} else {

  return 'You won!';

}

};

if (userChoice === 'scissor') {

  if (computerChoice === 'rock') {

    return 'The computer won!';

  } else {

    return 'You won!';

  }

};

if (userChoice === 'bomb'){

  return 'You won it all!';

};

};

const playGame = () => {

const userChoice = getUserChoice(‘paper’);

const computerChoice = getComputerChoice();

console.log(You threw: ${userChoice});

console.log(The computer threw: ${computerChoice});

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

};

playGame();

Sorry guys, i’m new to coding.
How will the userChoice change from ‘rock’ to paper’ to ‘scissors’ or vice versa?
How can we make this game come to reality?

you would need to prompt the user for input, and the validate this input. This can either be done through buttons (which mean you would need to interact with the DOM) or the prompt() function

Thanks stetim94 for the response.
Please can you show me?

depends what you are going for, the simplest solution is simply to use prompt:

let userChoice = prompt("Choice: rock, paper or scissors");

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