Question
I need a Javascript function that will work with this code to show the winner of a rock paper scissors game. my game Rounds to
I need a Javascript function that will work with this code to show the winner of a rock paper scissors game.
User Chose: Computer Chose:
JAVASCIPT:
function getElem(id) { return document.getElementById(id); }
var rounds;
function startGame() { rounds = getElem("ROUNDS_TO_PLAY"); rounds = parseInt(rounds.value);
document.roundsRemaining = rounds; getElem("ROUNDS_REMAINING").value = rounds; }
/* Responds to user choice button click. */ function userChoice(userButton) { updateStats(); displayUserChoice(userButton); var compChoice = getComputerChoice(); getElem("COMPUTER_CHOICE_OUTPUT").value = compChoice; displayComputerChoice(compChoice);
}
function updateStats() {
rounds=document.roundsRemaining; rounds--; document.roundsRemaining = rounds; getElem("ROUNDS_REMAINING").value = document.roundsRemaining;
if (document.roundsRemaining < 1) { alert("Game over"); getElem("ROCK_CHOICE").disabled=true; }
}
function getComputerChoice() { var r = Math.floor(Math.random() * 3)
switch (r) { case 0: return "ROCK"; case 1: return "PAPER"; case 2: return "SCISSORS"; default: console.log(r + " is not a valid computer choice."); }
}
function displayUserChoice(userButton) { var uco = getElem("USER_CHOICE_OUTPUT");
if (userButton == "ROCK") { uco.value = "ROCK"; } else if (userButton == "PAPER") { uco.value = "PAPER"; } else if (userButton == "SCISSORS") { uco.value = "SCISSORS"; } else { conosole.log(userButton + " is invalid!!"); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started