I am doing the codecademy course and had to do a rock paper scissors game as a project. Rather than following the instructions I tried Doing it on my own. How I could make the code more efficient?
//Only outputs 1-3
const COMPUTERCHOICE = (min=1, max=4) => {
return Math.floor(Math.random() * (max - min) + min); };
//Establishes a relation between the numbers and items
console.log('1 = Scissors, 2 = Paper, 3 = Rock\n')
function game(userChoice, computer) {
if (userChoice === 3 && computer === 1){
return `User chose ${userChoice}, Computer chose ${computer}, user Wins!`
}else if (computer === 3 && userChoice === 1){
return `User chose ${userChoice}, computer chose ${computer}, Computer Wins!`
}else if (userChoice < computer){
return `Player chose ${userChoice}, Computer Chose ${computer} User Wins! :D`;
} else if (userChoice === computer) {
return `It's a draw!`
}else {
return `Player chose ${userChoice}, Computer Chose ${computer} Computer wins :(`;
};
}
console.log(game(3, COMPUTERCHOICE()))
whatBeatsWhat[p1Choice] === p2Choice
. player2 wins ifwhatBeatsWhat[p2Choice] === p1Choice
. Otherwise, it's a tie. I find this to be a simple, readable solution. \$\endgroup\$