78 lines
No EOL
1.7 KiB
JavaScript
78 lines
No EOL
1.7 KiB
JavaScript
const ROCK = "rock";
|
|
const PAPER = "paper";
|
|
const SCISSORS = "scissors";
|
|
let ties = 0;
|
|
|
|
function getComputerChoice () {
|
|
let x = Math.floor(Math.random() * 3);
|
|
if (x === 1) {
|
|
return ROCK;
|
|
}
|
|
else if (x === 2) {
|
|
return PAPER;
|
|
}
|
|
else {
|
|
return SCISSORS;
|
|
}
|
|
}
|
|
|
|
function playRound () {
|
|
const computerSelection = getComputerChoice();
|
|
const playerSelection = (prompt("Rock, Paper or Scissors?")).toLowerCase();
|
|
if (playerSelection === computerSelection) {
|
|
alert("It's a tie!");
|
|
ties++;
|
|
}
|
|
else if (playerSelection === ROCK) {
|
|
if (computerSelection === PAPER) {
|
|
alert("You Lose! Paper beats Rock!");
|
|
return 0;
|
|
}
|
|
else {
|
|
alert("You Win! Rock beats Scissors!");
|
|
return 1;
|
|
}
|
|
}
|
|
else if (playerSelection === PAPER) {
|
|
if (computerSelection === ROCK) {
|
|
alert("You Win! Paper beats Rock!");
|
|
return 1;
|
|
}
|
|
else {
|
|
alert("You Lose! Scissors beat Paper!");
|
|
return 0;
|
|
}
|
|
}
|
|
else if (playerSelection === SCISSORS) {
|
|
if (computerSelection === ROCK) {
|
|
alert("You Lose! Rock beats Scissors!");
|
|
return 0;
|
|
}
|
|
else {
|
|
alert("You Win! Scissors beat Paper!");
|
|
return 1;
|
|
}
|
|
}
|
|
else {
|
|
alert("Invalid input!")
|
|
}
|
|
}
|
|
|
|
(function game() {
|
|
let wins = 0;
|
|
for (let i = 0; i < 5; i++) {
|
|
wins += playRound();
|
|
}
|
|
if (ties === 5) {
|
|
alert("You tied!")
|
|
return;
|
|
}
|
|
let losses = 5 - wins - ties;
|
|
if ( wins > losses) {
|
|
alert("You win!");
|
|
return;
|
|
}
|
|
else {
|
|
alert("You lose!")
|
|
}
|
|
})(); |