Help With Function's Random Outcome

Help With Function's Random Outcome
0

#1

Hello all, I’ve been working on a Rock, Paper, Scissors game and although it works I cannot get the results to change. I always ‘throw’ rock and the computer always ‘throws’ scissors unless I manually change the switch parameters. I’m looking to get the outcome to be random each time.

Below is my code, although I am NOT looking for anyone to just give me the answer. If you can point me in the right direction(s) that would be awesome :rofl: Any and all help is greatly appreciated! Have a great day and happy coding!

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

const getComputerChoice = () => {
  Math.floor(Math.random() * 1);
  switch (1) {
    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 === 'scissors')
    return "You win!";
	} else {
  return "You lose!";
	}

if (userChoice === 'paper') {
  if (computerChoice === 'rock');
  	return "You lose!";
	} else {
    return "You win!";
  }

if (userChoice === 'scissors') {
  if (computerChoice === 'paper');
  return "You win!";
} else {
  return "You lose!";
}
}


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

playGame();

#2

The problem is with your switch statement, you need to make the switch condition a variable, right now it is a constant

Edit: also I would add that you review how Math.random() works , while remembering that it only generates random numbers from 0 to 1 (0 inclusive, but not including 1)


#3

Much thanks for all your help, I hadn’t even looked at that. So basically set the condition to a variable (randomNumber)?

I appreciate the feedback about Math.random() as well!


#4

Use this pattern:

const anyOf = xs => xs[Math.floor(Math.random() * xs.length)]

const foo = anyOf(['rock', 'paper', 'scissors']) // each time returns random array element

#5

Yes, but not any randomNumber, but one that is within the range of choices that the computer should return, which in your case would be 3 choices. You are already on the correct path with this statement:
Math.floor(Math.random() * 1)

However, you can’t use it as is because, as I said before, Math.random() returns numbers between 0 to 1 , with the minimum being 0 and the maximum being 0.99999… while Math.floor() always returns the largest integer less than or equal to a given real number, therefore the statement above will always return 0.

So, figure out what to multiply Math.random() with before applying Math.floor() and you should be good to go.


#6

@0Prime - When a camper asks not to just give him/her the answer, see if you can present your response as a learning opportunity by given hints and references, so the camper can work out the solution on their own.

Thanks.