Rock, paper and scissors


#1

I am receiving this error

ReferenceError: prompt is not defined
but also, on the instructions, they didn’t say anything about prompt. I guess because they ask to console.log and execute the function at end. However, I’d appreciate some light regarding my choices here.
Thanks

let userPrompt = prompt("enter a choice: ");

const getUserChoice = userPrompt =>{
  if (userInput === 'rock' || userInput === 'Paper' || userInput === 'scissors') {
    return userInput;
 }else{
   console.log('Error');
 }
}

const randomNumber = 'Math.floor(Math.random() * 3)';
switch (randomNumber) {
  case 0:
    return 'rock';
    // I don't know if I have to have a break here.
  case 1:
    return 'Paper';
    
  case 2:
    return 'scissors';
  
  case 3:
    return 'bomb';
    
                    }
console.log(getUserChoice());
                    
function determineWinner(userChoice, computerChoice) {
  if (userChoice === computerChoice){
    return 'the game is a tie';
  } if (userChoice === 'bomb'){
      return 'You won!'; /* add a fourth condition that checks if the userInput is 'bomb'.At the beginning of determineWinner(), add another if statement that makes the user win if the userChoice is 'bomb'.*/
    } if (userChoice === 'rock'){
    if (computerChoice === 'paper') {
      return 'The computer won!';
    } else {
      return 'You won!';
    }
  } else if (userChoice === 'Paper') {
    if (computerChoice === 'rock'){
      return 'The user won';
    } else {
      return 'The computer won';
    }
  } else if (userChoice === 'Scissors') {
    if (computerChoice === 'Paper'){
      return 'The user won';
    } else {
      return 'The computer won';
    }
}}
 
console.log(determineWiner('rock', 'paper'));
console.log(determineWiner('paper', 'rock'));
console.log(determineWiner('scissors', 'paper'));

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();

#2

The optional break statement associated with each case label ensures that the program breaks out of switch once the matched statement is executed and continues execution at the statement following switch. If break is omitted, the program continues execution at the next statement in the switch statement.

I would suggest this:

// let userPrompt = prompt("enter a choice: ");

userPrompt = 'rock'

so you can focus on the other error messages in your code. like the getUserChoice function, which you gave a userPrompt parameter yet you use userInput in the condition, but userInput isn’t defined anywhere.


#3

Now my code works much better, but I have some undefined answers, do you know why?
How can I optimize the random action?
Thanks @stetim94

Rock
The computer won!
undefined
undefined
you threw: scissors
The computer threw: undefined
undefined

const getUserChoice = userInput =>{
  userInput = userInput.toLowerCase();
  if (userInput === 'rock' || userInput === 'Paper' || userInput === 'scissors') {
    return userInput;
 }else{
   console.log('Error');
 }
}

const getComputerChoice = randomNumber => {
  'Math.floor(Math.random() * 3)';
switch (randomNumber) {
  case 0:
    return 'rock';
    // I don't know if I have to have a break here.
  case 1:
    return 'Paper';
    
  case 2:
    return 'scissors';
  
  case 3:
    return 'bomb';
    
                    }}
console.log(getUserChoice('rock'));
                    
function determineWinner(userChoice, computerChoice) {
  if (userChoice === computerChoice){
    return 'the game is a tie';
  } if (userChoice === 'bomb'){
      return 'You won!'; /* add a fourth condition that checks if the userInput is 'bomb'.At the beginning of determineWinner(), add another if statement that makes the user win if the userChoice is 'bomb'.*/
    } if (userChoice === 'rock'){
    if (computerChoice === 'paper') {
      return 'The computer won!';
    } else {
      return 'You won!';
    }
  } else if (userChoice === 'Paper') {
    if (computerChoice === 'rock'){
      return 'The user won';
    } else {
      return 'The computer won';
    }
  } else if (userChoice === 'Scissors') {
    if (computerChoice === 'Paper'){
      return 'The user won';
    } else {
      return 'The computer won';
    }
}}
 
console.log(determineWinner('rock', 'paper'));
console.log(determineWinner('paper', 'rock'));
console.log(determineWinner('scissors', 'paper'));

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

  
  
  playGame();

#4

string comparison is case sensitive, here:

if (computerChoice === 'Paper')

you use Paper, which won’t equal paper, as such, no condition of determineWinner is true, so no return keyword is reached, so undefined is returned (default value)