I am working on a coding task to coding a Rock, Paper, Scissors game.
I have written some functions already to handle to computer choice and user choice, and I’m also supposed to write a function to call the other functions and also keep track of the scores.
I am stuck without a way of bring everything together. This is my code below.
let input;
let random;
function getComputerChoice(min = 1, max = 3){
min = Math.ceil(min);
max = Math.floor(max);
random = Math.floor(Math.random() * (max - min + 1) + min);
if (random === 1){
console.log("computer picked Rock");
return random;
}
else if (random === 2){
console.log("computer picked Paper");
return random;
}
else
random === 3;
console.log("computer picked Scissors");
return random;
}
function playerSelection(){
input = prompt("Input Rock, Paper or Scissors").toLowerCase();
console.log("you picked", input);
return input;
}
function playRound(yourSelection, computerSelection) {
// your code here!
if (yourSelection === "rock" && computerSelection === 3){
console.log("you win")
}
else if (yourSelection === "scissors" && computerSelection === 2){
console.log("you win")
}
else if (yourSelection === "paper" && computerSelection === 1){
console.log("you win")
}
else
console.log("You lose, try again")
}
const yourSelection = playerSelection();
const computerSelection = getComputerChoice();
playRound(yourSelection, computerSelection);
function game(){
var theGame = game()
}
2
Answers
It looks like you’re off to a good start with your rock-paper-scissors game, but there are some issues in your code that need to be addressed. Here is a corrected version of your code:
In your
game
function you’d need a loop to run a round and save the scores. If you want to end the game enter "exit".