Skip to content

Instantly share code, notes, and snippets.

@SinachPat
Created January 16, 2022 18:29
Show Gist options
  • Save SinachPat/9a009fc511d5bf3be6669cc9d16f9e5e to your computer and use it in GitHub Desktop.
Save SinachPat/9a009fc511d5bf3be6669cc9d16f9e5e to your computer and use it in GitHub Desktop.
let choices = ["Rock", "Paper", "Scissors"];
function computerPlay(choices) {
let option = choices[Math.floor(Math.random()*choices.length)];
return option;
}
// computerPlay(choices);
function oneRound(playerSelection, computerSelection) {
if (playerSelection == "rock" && computerSelection == "rock") {
console.log("It's a tie! rock holds rock");
}
else if (playerSelection == "rock" && computerSelection == "paper"){
console.log("You lose! paper covers rock");
}
else if (playerSelection == "rock" && computerSelection == "scissors") {
console.log("You win! rock beats scissors");
}
else if (playerSelection == "scissors" && computerSelection == "rock") {
console.log("You lose! rock beats scissors");
}
else if (playerSelection == "paper" && computerSelection == "rock") {
console.log("You win! paper covers rock")
}
else if (playerSelection == "paper" && computerSelection == "paper") {
console.log("It's a tie!");
}
else if (playerSelection == "paper" && computerSelection == "scissors") {
console.log("You lose! scissors beats paper")
}
else if (playerSelection == "scissors" && computerSelection == "scissors") {
console.log("It's a tie");
}
else if (playerSelection == "scissors" && computerSelection == "paper") {
console.log("You win! scissors beats paper")
}
else {
console.log("There was no play!")
}
}
function game() {
let playerScore = 0;
let computerScore = 0;
let totalRounds = 5;
for (i = 0; i <= totalRounds; i++) {
oneRound(playerSelection,computerSelection);
if(playerSelection == "rock" && computerSelection == "scissors" || playerSelection == "paper" && computerSelection == "rock" || playerSelection == "scissors" && computerSelection == "paper"){
playerScore++;
}
else if(playerSelection == "rock" && computerSelection == "paper" || playerSelection == "scissors" && computerSelection == "rock" || playerSelection == "paper" && computerSelection == "scissors") {
computerScore++;
}
else if(playerSelection == "scissors" && computerSelection == "scissors" || playerSelection == "paper" && computerSelection == "paper" || playerSelection == "rock" && computerSelection == "rock") {
"";
}
else {
console.log("We play again!");
}
}
console.log(playerScore);
console.log(computerScore);
}
let playerSelection = prompt("What do you play").toLowerCase();
let computerSelection = computerPlay(choices).toLowerCase();
// oneRound(playerSelection,computerSelection)
game();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment