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